<html>
<head><meta charset="utf-8"><title>Optimizing a match on a just-constructed enum #85133 · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html">Optimizing a match on a just-constructed enum #85133</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="238500132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238500132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238500132">(May 12 2021 at 16:04)</a>:</h4>
<p>I've started trying to implement something, but I'm having trouble generating the MIR dump as per step 2 of the guide on creating a new optimization. When I add <code>// EMIT_MIR simplify_obvious_switch.diff</code> above the function I want a dump of, it gives me an error about wanting files to exist:</p>
<div class="codehilite"><pre><span></span><code>Output file `/home/theodegioanni/github/rust/build/x86_64-unknown-linux-gnu/test/mir-opt/simplify_obvious_switch/simplify_obvious_switch.mir` from test does not exist, available files are in `/home/theodegioanni/github/rust/build/x86_64-unknown-linux-gnu/test/mir-opt/simplify_obvious_switch`
</code></pre></div>
<p>but the thing is that I want it to generate those files, right? I might be misunderstanding, could somebody shed light upon this?</p>



<a name="238500488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238500488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238500488">(May 12 2021 at 16:06)</a>:</h4>
<p>Doesn't the ConstGoto optimization already perform half of the objective?</p>



<a name="238500953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238500953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238500953">(May 12 2021 at 16:09)</a>:</h4>
<p>oh well</p>



<a name="238500962"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238500962" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238500962">(May 12 2021 at 16:09)</a>:</h4>
<p>it's doing exactly what I wanted to add</p>



<a name="238501099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238501099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238501099">(May 12 2021 at 16:10)</a>:</h4>
<p>hmm actually</p>



<a name="238501155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238501155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238501155">(May 12 2021 at 16:10)</a>:</h4>
<p>why does ConstGoto stick to gotos?</p>



<a name="238501180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238501180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238501180">(May 12 2021 at 16:10)</a>:</h4>
<p>it could do what it does on any terminator, right?</p>



<a name="238501542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238501542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238501542">(May 12 2021 at 16:12)</a>:</h4>
<p>IIRC, ConstGoto was meant to be a fast version of the constant propagation and destination propagation, which can propagate across arbitrary terminators.</p>



<a name="238501967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238501967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238501967">(May 12 2021 at 16:15)</a>:</h4>
<p>is it? I'm having trouble visualizing the link, I'll give it more thought</p>



<a name="238502065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238502065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238502065">(May 12 2021 at 16:16)</a>:</h4>
<p>ah never mind i'm a dumdum</p>



<a name="238502764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238502764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238502764">(May 12 2021 at 16:20)</a>:</h4>
<p>weirdly enough, with opt-level 3, this does not seem to get optimized:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">match</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="nb">Ok</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(</span><span class="n">v</span><span class="p">),</span><span class="w"></span>
<span class="w">            </span><span class="nb">Err</span><span class="p">(</span><span class="n">r</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ControlFlow</span>::<span class="n">Break</span><span class="p">(</span><span class="n">r</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">ControlFlow</span>::<span class="n">Continue</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">v</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="n">ControlFlow</span>::<span class="n">Break</span><span class="p">(</span><span class="n">r</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="238502832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238502832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238502832">(May 12 2021 at 16:20)</a>:</h4>
<p>we'd only need one switch yet two are generated</p>



<a name="238503752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238503752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238503752">(May 12 2021 at 16:26)</a>:</h4>
<p>ConstGoto is gated on level 4, isn't it?</p>



<a name="238503943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238503943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238503943">(May 12 2021 at 16:28)</a>:</h4>
<p>I tried level 4, same issue</p>



<a name="238503950"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238503950" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238503950">(May 12 2021 at 16:28)</a>:</h4>
<p><a href="https://rust.godbolt.org/z/WcaWoKhTr">https://rust.godbolt.org/z/WcaWoKhTr</a></p>



<a name="238503954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238503954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238503954">(May 12 2021 at 16:28)</a>:</h4>
<p>tell me what you think</p>



<a name="238505272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238505272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238505272">(May 12 2021 at 16:36)</a>:</h4>
<p>There's something fishy. With this level of optimizations, the RemoveStorageMarkers pass should trigger.</p>



<a name="238505780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238505780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238505780">(May 12 2021 at 16:40)</a>:</h4>
<p>if we've been staring at unoptimized MIR from the beginning I'm going to be sad</p>



<a name="238506288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238506288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238506288">(May 12 2021 at 16:44)</a>:</h4>
<p>What does a local build give you?</p>



<a name="238506916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238506916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238506916">(May 12 2021 at 16:48)</a>:</h4>
<p>a local build gives me the exact same thing</p>



<a name="238506935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238506935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238506935">(May 12 2021 at 16:48)</a>:</h4>
<p>my nightly is probably something like 3 days old</p>



<a name="238507349"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238507349" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238507349">(May 12 2021 at 16:51)</a>:</h4>
<p>Ok, my bad. RemoveStorageMarkers is only enabled with -C opt-level=0, because it removes annotations that LLVM needs.</p>



<a name="238507418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238507418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238507418">(May 12 2021 at 16:52)</a>:</h4>
<p>stable gives relatively the same thing however</p>



<a name="238507566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238507566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238507566">(May 12 2021 at 16:53)</a>:</h4>
<p>ConstGoto is currently limited to blocks without any statement. Lifting this limitation for blocks with a <em>few</em> statements may be the optimization you are looking for (with <em>few</em> to define).</p>



<a name="238513951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238513951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238513951">(May 12 2021 at 17:36)</a>:</h4>
<p>Hmm, looking at <code>const_goto</code> it also says "// We only apply this optimization if the <em>last statement</em> is a const assignment" -- I guess that's why the conversation above about the <code>StorageDead</code>s?</p>



<a name="238520311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238520311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238520311">(May 12 2021 at 18:18)</a>:</h4>
<p>Maybe just copy <code>const_goto</code> for now to special case it to discriminants -- "if the block sets a discriminant then gotos to a block that switchInts on that same discriminant, we copy the target block onto the tail of the source block" or something.</p>
<p>Feels like that should be safe, because even if something weird ends up overwriting that discriminant afterwards in the block it'd still do the right thing (and for easy cases the const prop will later fix the switch into a goto).  And might help for a whole bunch of things like inlining functions that return options -- even something like <code>match some_iter.next(...)</code> would be better with this since it'd CFG rewrite <code>return None</code> to go to the correct place directly...</p>



<a name="238522999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238522999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238522999">(May 12 2021 at 18:35)</a>:</h4>
<p>Surely the discriminant not being touched should be enough for it to be sound on any code block?</p>



<a name="238523161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238523161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238523161">(May 12 2021 at 18:36)</a>:</h4>
<p>Ah actually I’m neglecting references</p>



<a name="238523231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238523231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238523231">(May 12 2021 at 18:37)</a>:</h4>
<p>I don’t know how modifying a local via a reference is translated in MIR</p>



<a name="238523506"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238523506" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238523506">(May 12 2021 at 18:38)</a>:</h4>
<p><code>*_4</code> is a place if <code>_4</code> is a reference.</p>



<a name="238523719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238523719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238523719">(May 12 2021 at 18:40)</a>:</h4>
<p>But tracking aliasing is probably not worth it most of the time in mir opts, when just copying the target block is easy enough.</p>



<a name="238524306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524306">(May 12 2021 at 18:44)</a>:</h4>
<p>Oh, here's another nice test case for you: <a href="https://rust.godbolt.org/z/oGz64vKjh">https://rust.godbolt.org/z/oGz64vKjh</a></p>
<p>Even with <code>-Z unsound-mir-opts=y</code> it's still a non-optimal CFG.</p>
<p>(People probably wouldn't write that manually, but after inlining I could totally see it.)</p>



<a name="238524325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524325">(May 12 2021 at 18:44)</a>:</h4>
<p>I meant that if you have something like</p>
<div class="codehilite"><pre><span></span><code>_2 = &amp;mut _3;
*_2 = whatever
</code></pre></div>
<p>it’s hard to track changes to _3</p>



<a name="238524444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524444">(May 12 2021 at 18:45)</a>:</h4>
<p>Wouldn’t your test case compile to the same as the chained match?</p>



<a name="238524637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524637">(May 12 2021 at 18:46)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409735">Théo Degioanni</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Suggestions.20for.20a.20beginner/near/238524444">said</a>:</p>
<blockquote>
<p>Wouldn’t your test case compile to the same as the chained match?</p>
</blockquote>
<p>Yeah, it basically does; I just think it's a more realistic situation to hit outside of <code>?</code>.</p>



<a name="238524638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524638">(May 12 2021 at 18:46)</a>:</h4>
<p>By the way, would someone mind explaining how optimizations are tested? I’m having trouble understanding the testing setup</p>



<a name="238524763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524763">(May 12 2021 at 18:47)</a>:</h4>
<p>I can try to help, but probably don't know any nuances.  It's been a while since I wrote a mir-opt, and even then I think I just copied whatever some other test was doing.</p>



<a name="238524843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524843">(May 12 2021 at 18:47)</a>:</h4>
<p>Right. I seem to struggle even with that lol</p>



<a name="238524879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238524879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238524879">(May 12 2021 at 18:48)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409735">Théo Degioanni</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Suggestions.20for.20a.20beginner/near/238524325">said</a>:</p>
<blockquote>
<p>I meant that if you have something like</p>
<div class="codehilite"><pre><span></span><code>_2 = &amp;mut _3;
*_2 = whatever
</code></pre></div>
<p>it’s hard to track changes to _3</p>
</blockquote>
<p>Agreed.  That's why I was saying to just copy the other block, not to try to actually lookup the const in the <code>switchInt</code> and simplify it further as part of the pass.</p>



<a name="238525022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238525022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238525022">(May 12 2021 at 18:48)</a>:</h4>
<p>The "diff" files in mir-opt-tests are new from last I wrote anything in mir-opt too.  (Nice, though!  Way easier to read what it's trying to test.)</p>



<a name="238525199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238525199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238525199">(May 12 2021 at 18:50)</a>:</h4>
<p>(I can’t seem to be able to answer to specific posts on mobiles, so here is an answer to the "Agreed" message): I don’t understand what you mean. have you read the message I sent to you on discord by te way? I had not Zulip installed on my phone at that time</p>



<a name="238525364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238525364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238525364">(May 12 2021 at 18:50)</a>:</h4>
<p>Oh I think I just understood how to get started. I think the book is outdated.</p>



<a name="238525390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238525390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238525390">(May 12 2021 at 18:50)</a>:</h4>
<p>You have to make the diff manually</p>



<a name="238525428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238525428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238525428">(May 12 2021 at 18:51)</a>:</h4>
<p>And the test framework actually uses it for testing</p>



<a name="238525862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238525862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238525862">(May 12 2021 at 18:54)</a>:</h4>
<p>Oh, no, haven't checked discord lately.</p>



<a name="238526006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526006">(May 12 2021 at 18:55)</a>:</h4>
<p>Yeah sorry to have split the convo, I was really exited to get that message to you lol</p>



<a name="238526124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526124">(May 12 2021 at 18:55)</a>:</h4>
<p>Oh, yeah, so your message there is basically what I said <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>
<p>But I wasn't thinking of including the "take the block that value would lead to" because that requires knowing there's no aliasing problems.</p>



<a name="238526242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526242">(May 12 2021 at 18:56)</a>:</h4>
<p>Yeah right</p>



<a name="238526287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526287">(May 12 2021 at 18:56)</a>:</h4>
<p>I guess it could be done with some extra restrictions like "and there's no other writes between the write and switching on it"</p>



<a name="238526323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526323">(May 12 2021 at 18:56)</a>:</h4>
<p>I guess that would work</p>



<a name="238526463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526463">(May 12 2021 at 18:57)</a>:</h4>
<p>but I think that if this pass just removes the arrow from the CFG by duplicating the <code>switchInt</code>, the existing const prop pass will turn the <code>switchInt</code> into a <code>goto</code>.</p>



<a name="238526540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526540">(May 12 2021 at 18:58)</a>:</h4>
<p>Yes I was about to suggest that</p>



<a name="238526548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526548">(May 12 2021 at 18:58)</a>:</h4>
<p>And that pass is way smarter than this one would have to be -- it'd even pick it up if written through a mutable reference or raw pointer and such.</p>



<a name="238526580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526580">(May 12 2021 at 18:58)</a>:</h4>
<p>Yeah that sounds very good</p>



<a name="238526851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526851">(May 12 2021 at 19:00)</a>:</h4>
<p>Now to detect where to apply the opt in a meaningful way</p>



<a name="238526951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238526951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238526951">(May 12 2021 at 19:00)</a>:</h4>
<p>And, well, "remove an arrow by copying the contents of the target block" seems like the correctness would be way more obvious than something smart <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="238527005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238527005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238527005">(May 12 2021 at 19:00)</a>:</h4>
<p>Yeah right</p>



<a name="238527080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238527080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238527080">(May 12 2021 at 19:01)</a>:</h4>
<p>I developed a curiosity about formally verifying MIR passes</p>



<a name="238527115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238527115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238527115">(May 12 2021 at 19:01)</a>:</h4>
<p>It’s not completely unreasonable, which in a way is impressive</p>



<a name="238527330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238527330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238527330">(May 12 2021 at 19:03)</a>:</h4>
<p>Anyway that’s the way to go. I’ll try my new understanding of the testing set up and get to work</p>



<a name="238527376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238527376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238527376">(May 12 2021 at 19:03)</a>:</h4>
<p>Yeah, kudos to Ralf and co for trying to make MIR good for such things -- ferrocene is looking at using it as the validation target too.</p>



<a name="238527435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238527435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238527435">(May 12 2021 at 19:03)</a>:</h4>
<p>Good luck!</p>



<a name="238723444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238723444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238723444">(May 14 2021 at 06:15)</a>:</h4>
<p>Figured out the testing setup! <span aria-label="tada" class="emoji emoji-1f389" role="img" title="tada">:tada:</span></p>



<a name="238723806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238723806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238723806">(May 14 2021 at 06:21)</a>:</h4>
<p>Hmmm... I'm afraid we might have to do <em>some</em> change tracking.</p>



<a name="238723844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238723844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238723844">(May 14 2021 at 06:21)</a>:</h4>
<p>Because there's still one indirection from the enum to the place storing the determinant.</p>



<a name="238723924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238723924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238723924">(May 14 2021 at 06:22)</a>:</h4>
<p>Now I think I can do basic tracking and whitelist the determinant as an edge that is accepted as side-effect free, but that's too bad.</p>



<a name="238723941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238723941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238723941">(May 14 2021 at 06:23)</a>:</h4>
<p>This is what I'll do for now.</p>



<a name="238724909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238724909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238724909">(May 14 2021 at 06:36)</a>:</h4>
<p>Actually I could also do a coarse estimation and duplicate aggressively. How bad is it to abusively duplicate code in situations where it might not actually be useful, in the hope it helps propagating consts?</p>



<a name="238729855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238729855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238729855">(May 14 2021 at 07:35)</a>:</h4>
<p>It's a tradeoff. For instance, if all the duplicated statements are storagelive/storagedead/nop, this is ok. If you are duplicating tens of non-trivial statements, its probably not worth it.</p>



<a name="238730043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238730043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238730043">(May 14 2021 at 07:37)</a>:</h4>
<p>Okay. I'll attempt that but with conservative (tweakable) limitations so it does not explode in complexity.</p>



<a name="238735677"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238735677" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238735677">(May 14 2021 at 08:38)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span> I checked something I forgot to check before in godbolt. The <code>too_complex</code> test and the one you suggested both get remarkably well optimized by LLVM later down the line, here is for the first one:</p>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code><span class="nl">too_complex:</span>
        <span class="nf">mov</span>     <span class="no">edx</span><span class="p">,</span> <span class="no">esi</span>
        <span class="nf">xor</span>     <span class="no">eax</span><span class="p">,</span> <span class="no">eax</span>
        <span class="nf">test</span>    <span class="no">edi</span><span class="p">,</span> <span class="no">edi</span>
        <span class="nf">sete</span>    <span class="no">al</span>
        <span class="nf">ret</span>
</code></pre></div>
<p>which makes me question the necessity of the optimization. Considering it's not really a compile-time-oriented optimization, is it worth doing in MIR?</p>



<a name="238793939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238793939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238793939">(May 14 2021 at 16:34)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409735">Théo Degioanni</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/238735677">said</a>:</p>
<blockquote>
<p>which makes me question the necessity of the optimization.</p>
</blockquote>
<p>So, here's the most direct demo of how it's valuable: <a href="https://rust.godbolt.org/z/45jMGTPaE">https://rust.godbolt.org/z/45jMGTPaE</a></p>
<p>Basically that <code>test</code>+<code>sete</code> _is_ pretty good, but it's still not the nothing that it could be.</p>



<a name="238794191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238794191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238794191">(May 14 2021 at 16:36)</a>:</h4>
<p>Unfortunately (as I understand it) LLVM really like losing the range information from <code>switchInt [ 0 =&gt; bb1, 1 =&gt; bb2, _ =&gt; unreachable ]</code>, turning it into just a two-way "was it zero?" branch, and thus not being able to just copy over the discriminant like it ought to, which is why doing this in MIR is useful.</p>



<a name="238828124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238828124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238828124">(May 14 2021 at 20:53)</a>:</h4>
<p>Okay, thank you for that info. I think I’ll make the test case exactly that.</p>



<a name="238829341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238829341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238829341">(May 14 2021 at 21:03)</a>:</h4>
<p>That test is basically <a href="https://github.com/rust-lang/rust/blob/master/src/test/codegen/try_identity.rs">https://github.com/rust-lang/rust/blob/master/src/test/codegen/try_identity.rs</a> -- so I'm not sure there's a good way to run it in your tree unless you either disable <code>SimplifyArmIdentity</code> or rebase atop my PR that changes <code>?</code>.</p>



<a name="238830561"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238830561" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238830561">(May 14 2021 at 21:12)</a>:</h4>
<p>For now I'll stick to the example test, I think adding the ? desugar specifically as a test can be done in a second pass</p>



<a name="238830669"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238830669" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238830669">(May 14 2021 at 21:13)</a>:</h4>
<p>By the way, what's the best way to obtain the parents of a bb?</p>



<a name="238830692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238830692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238830692">(May 14 2021 at 21:13)</a>:</h4>
<p>I can't find any example of that in other passes weirdly enough</p>



<a name="238830698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238830698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238830698">(May 14 2021 at 21:13)</a>:</h4>
<p>Or maybe I don't see them as what they are...</p>



<a name="238830969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238830969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238830969">(May 14 2021 at 21:14)</a>:</h4>
<p>I think there's a post-order iterator for BBs, but I don't know if there's a simple table kind of thing for it.</p>



<a name="238831080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238831080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238831080">(May 14 2021 at 21:15)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/graph/iterate/fn.post_order_walk.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/graph/iterate/fn.post_order_walk.html</a></p>



<a name="238831215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238831215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238831215">(May 14 2021 at 21:17)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#method.predecessors">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/struct.Body.html#method.predecessors</a></p>



<a name="238831355"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238831355" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238831355">(May 14 2021 at 21:18)</a>:</h4>
<p>I feel silly for not being able to find that. Thank you.</p>



<a name="238831414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238831414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238831414">(May 14 2021 at 21:18)</a>:</h4>
<p>though do take care about calling it often while modifying MIR at the same time – its quite expensive to recompute and is thus cached. And modifications do invalidate the cache.</p>



<a name="238831759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238831759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238831759">(May 14 2021 at 21:22)</a>:</h4>
<p>I see optimizations use a visitor pattern instead of simply iterating over blocks. What are the advantages of that?</p>



<a name="238831850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238831850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238831850">(May 14 2021 at 21:23)</a>:</h4>
<p>for example ConstGoto uses a visitor on terminators, but it could just as well iterate over blocks and look at the terminators</p>



<a name="238888309"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238888309" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238888309">(May 15 2021 at 10:48)</a>:</h4>
<p>I think I've successfully made a function that approximates what places determine the value of a place within a single block. I hope that was not duplicate effort. Was it?</p>



<a name="238917573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238917573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238917573">(May 15 2021 at 18:44)</a>:</h4>
<p>I think the optimization is working, but when building the test setup i'm hitting <a href="https://github.com/rust-lang/rust/blob/eac3c7c5bd38ec38062ebde475bd2ea6317d0c09/compiler/rustc_codegen_ssa/src/mir/analyze.rs#L394">https://github.com/rust-lang/rust/blob/eac3c7c5bd38ec38062ebde475bd2ea6317d0c09/compiler/rustc_codegen_ssa/src/mir/analyze.rs#L394</a></p>



<a name="238917586"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238917586" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238917586">(May 15 2021 at 18:45)</a>:</h4>
<p>I have no idea what it is or why it  even is an issue that something would have 2 parents, does somebody have an explanation?</p>



<a name="238917875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238917875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238917875">(May 15 2021 at 18:50)</a>:</h4>
<p>What does the MIR look like?</p>



<a name="238917908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238917908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238917908">(May 15 2021 at 18:50)</a>:</h4>
<p>I don't really know, because I'm getting this while building stage1 I believe</p>



<a name="238917921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238917921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238917921">(May 15 2021 at 18:51)</a>:</h4>
<p>Is there a way to see the MIR in that context?</p>



<a name="238918003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238918003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238918003">(May 15 2021 at 18:52)</a>:</h4>
<p>what I've been doing is copying blocks so that's probably how a "two parents" situation would arise</p>



<a name="238918085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238918085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238918085">(May 15 2021 at 18:54)</a>:</h4>
<p>I managed to run stage0 on an example</p>



<a name="238918127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238918127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238918127">(May 15 2021 at 18:55)</a>:</h4>
<div class="codehilite"><pre><span></span><code>// WARNING: This output format is intended for human consumers only
// and is subject to change without notice. Knock yourself out.
fn main() -&gt; () {
    let mut _0: ();                      // return place in scope 0 at src/test/mir-opt/separate_const_switch.rs:26:11: 26:11
    let _1: std::option::Option&lt;i32&gt;;    // in scope 0 at src/test/mir-opt/separate_const_switch.rs:27:5: 27:35
    let mut _2: std::result::Result&lt;i32, u32&gt;; // in scope 0 at src/test/mir-opt/separate_const_switch.rs:27:29: 27:34

    bb0: {
        StorageLive(_1);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:5: 27:35
        StorageLive(_2);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:29: 27:34
        ((_2 as Ok).0: i32) = const 0_i32; // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:29: 27:34
        discriminant(_2) = 0;            // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:29: 27:34
        _1 = too_complex::&lt;i32, u32&gt;(move _2) -&gt; bb1; // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:5: 27:35
                                         // mir::Constant
                                         // + span: src/test/mir-opt/separate_const_switch.rs:27:5: 27:28
                                         // + literal: Const { ty: fn(std::result::Result&lt;i32, u32&gt;) -&gt; std::option::Option&lt;i32&gt; {too_complex::&lt;i32, u32&gt;}, val: Value(Scalar(&lt;ZST&gt;)) }
    }

    bb1: {
        StorageDead(_2);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:34: 27:35
        StorageDead(_1);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:27:35: 27:36
        return;                          // scope 0 at src/test/mir-opt/separate_const_switch.rs:28:2: 28:2
    }
}

fn too_complex(_1: Result&lt;T, E&gt;) -&gt; Option&lt;T&gt; {
    debug x =&gt; _1;                       // in scope 0 at src/test/mir-opt/separate_const_switch.rs:7:22: 7:23
    let mut _0: std::option::Option&lt;T&gt;;  // return place in scope 0 at src/test/mir-opt/separate_const_switch.rs:7:42: 7:51
    let mut _2: std::result::Result&lt;E, T&gt;; // in scope 0 at src/test/mir-opt/separate_const_switch.rs:15:11: 20:6
    let mut _3: isize;                   // in scope 0 at src/test/mir-opt/separate_const_switch.rs:17:13: 17:18
    let _4: T;                           // in scope 0 at src/test/mir-opt/separate_const_switch.rs:17:16: 17:17
    let mut _5: T;                       // in scope 0 at src/test/mir-opt/separate_const_switch.rs:17:26: 17:27
    let _6: E;                           // in scope 0 at src/test/mir-opt/separate_const_switch.rs:18:17: 18:18
    let mut _7: E;                       // in scope 0 at src/test/mir-opt/separate_const_switch.rs:18:26: 18:27
    let mut _8: isize;                   // in scope 0 at src/test/mir-opt/separate_const_switch.rs:21:9: 21:15
    let _9: T;                           // in scope 0 at src/test/mir-opt/separate_const_switch.rs:21:13: 21:14
    let mut _10: T;                      // in scope 0 at src/test/mir-opt/separate_const_switch.rs:21:24: 21:25
    let _11: E;                          // in scope 0 at src/test/mir-opt/separate_const_switch.rs:22:12: 22:13
    scope 1 {
        debug v =&gt; _4;                   // in scope 1 at src/test/mir-opt/separate_const_switch.rs:17:16: 17:17
    }
    scope 2 {
        debug r =&gt; _6;                   // in scope 2 at src/test/mir-opt/separate_const_switch.rs:18:17: 18:18
    }
    scope 3 {
        debug v =&gt; _9;                   // in scope 3 at src/test/mir-opt/separate_const_switch.rs:21:13: 21:14
    }
    scope 4 {
        debug r =&gt; _11;                  // in scope 4 at src/test/mir-opt/separate_const_switch.rs:22:12: 22:13
    }

    bb0: {
        StorageLive(_2);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:15:11: 20:6
        _3 = discriminant(_1);           // scope 0 at src/test/mir-opt/separate_const_switch.rs:17:13: 17:18
        switchInt(move _3) -&gt; [0_isize: bb3, 1_isize: bb1, otherwise: bb2]; // scope 0 at src/test/mir-opt/separate_const_switch.rs:17:13: 17:18
    }

    bb1: {
        StorageLive(_6);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:18:17: 18:18
        _6 = move ((_1 as Err).0: E);    // scope 0 at src/test/mir-opt/separate_const_switch.rs:18:17: 18:18
        StorageLive(_7);                 // scope 2 at src/test/mir-opt/separate_const_switch.rs:18:26: 18:27
        _7 = move _6;                    // scope 2 at src/test/mir-opt/separate_const_switch.rs:18:26: 18:27
        ((_2 as Ok).0: E) = move _7;     // scope 2 at src/test/mir-opt/separate_const_switch.rs:18:23: 18:28
        discriminant(_2) = 0;            // scope 2 at src/test/mir-opt/separate_const_switch.rs:18:23: 18:28
        StorageDead(_7);                 // scope 2 at src/test/mir-opt/separate_const_switch.rs:18:27: 18:28
        StorageDead(_6);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:18:27: 18:28
        goto -&gt; bb4;                     // scope 0 at src/test/mir-opt/separate_const_switch.rs:16:9: 19:10
    }

    bb2: {
        unreachable;                     // scope 0 at src/test/mir-opt/separate_const_switch.rs:16:15: 16:16
    }

    bb3: {
        StorageLive(_4);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:17:16: 17:17
        _4 = move ((_1 as Ok).0: T);     // scope 0 at src/test/mir-opt/separate_const_switch.rs:17:16: 17:17
        StorageLive(_5);                 // scope 1 at src/test/mir-opt/separate_const_switch.rs:17:26: 17:27
        _5 = move _4;                    // scope 1 at src/test/mir-opt/separate_const_switch.rs:17:26: 17:27
        ((_2 as Err).0: T) = move _5;    // scope 1 at src/test/mir-opt/separate_const_switch.rs:17:22: 17:28
        discriminant(_2) = 1;            // scope 1 at src/test/mir-opt/separate_const_switch.rs:17:22: 17:28
        StorageDead(_5);                 // scope 1 at src/test/mir-opt/separate_const_switch.rs:17:27: 17:28
        StorageDead(_4);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:17:27: 17:28
        goto -&gt; bb4;                     // scope 0 at src/test/mir-opt/separate_const_switch.rs:16:9: 19:10
    }

    bb4: {
        _8 = discriminant(_2);           // scope 0 at src/test/mir-opt/separate_const_switch.rs:21:9: 21:15
        switchInt(move _8) -&gt; [0_isize: bb5, 1_isize: bb7, otherwise: bb6]; // scope 0 at src/test/mir-opt/separate_const_switch.rs:21:9: 21:15
    }

    bb5: {
        StorageLive(_11);                // scope 0 at src/test/mir-opt/separate_const_switch.rs:22:12: 22:13
        _11 = move ((_2 as Ok).0: E);    // scope 0 at src/test/mir-opt/separate_const_switch.rs:22:12: 22:13
        discriminant(_0) = 0;            // scope 4 at src/test/mir-opt/separate_const_switch.rs:22:18: 22:22
        drop(_11) -&gt; bb8;                // scope 0 at src/test/mir-opt/separate_const_switch.rs:22:21: 22:22
    }

    bb6: {
        unreachable;                     // scope 0 at src/test/mir-opt/separate_const_switch.rs:15:11: 20:6
    }

    bb7: {
        StorageLive(_9);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:21:13: 21:14
        _9 = move ((_2 as Err).0: T);    // scope 0 at src/test/mir-opt/separate_const_switch.rs:21:13: 21:14
        StorageLive(_10);                // scope 3 at src/test/mir-opt/separate_const_switch.rs:21:24: 21:25
        _10 = move _9;                   // scope 3 at src/test/mir-opt/separate_const_switch.rs:21:24: 21:25
        ((_0 as Some).0: T) = move _10;  // scope 3 at src/test/mir-opt/separate_const_switch.rs:21:19: 21:26
        discriminant(_0) = 1;            // scope 3 at src/test/mir-opt/separate_const_switch.rs:21:19: 21:26
        StorageDead(_10);                // scope 3 at src/test/mir-opt/separate_const_switch.rs:21:25: 21:26
        StorageDead(_9);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:21:25: 21:26
        goto -&gt; bb9;                     // scope 0 at src/test/mir-opt/separate_const_switch.rs:15:5: 23:6
    }

    bb8: {
        StorageDead(_11);                // scope 0 at src/test/mir-opt/separate_const_switch.rs:22:21: 22:22
        goto -&gt; bb9;                     // scope 0 at src/test/mir-opt/separate_const_switch.rs:15:5: 23:6
    }

    bb9: {
        StorageDead(_2);                 // scope 0 at src/test/mir-opt/separate_const_switch.rs:24:1: 24:2
        return;                          // scope 0 at src/test/mir-opt/separate_const_switch.rs:24:2: 24:2
    }
}
</code></pre></div>



<a name="238918144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238918144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238918144">(May 15 2021 at 18:55)</a>:</h4>
<p>this is what I get running stage0 on the example</p>



<a name="238918156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238918156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238918156">(May 15 2021 at 18:55)</a>:</h4>
<p>I don't think the optimization triggered tho, I ran <code>rustc +stage0 src/test/mir-opt/separate_const_switch.rs --emit=mir -O</code><br>
edit: yeah that's not running it</p>



<a name="238919735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238919735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238919735">(May 15 2021 at 19:23)</a>:</h4>
<p>I should also mention that this is not the one that errors out. I don’t know how to retrieve that one specifically as the error comes from within the stage1 build process</p>



<a name="238921066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921066">(May 15 2021 at 19:47)</a>:</h4>
<div class="codehilite"><pre><span></span><code>  Compiling test v0.0.0 (/home/theodegioanni/github/rust/library/test)
error: internal compiler error: compiler/rustc_codegen_ssa/src/mir/analyze.rs:394:21: funclet bb235 has 2 parents - bb221 and bb273
   --&gt; library/test/src/lib.rs:201:1
    |
201 | / pub fn run_tests&lt;F&gt;(
202 | |     opts: &amp;TestOpts,
203 | |     tests: Vec&lt;TestDescAndFn&gt;,
204 | |     mut notify_about_test_event: F,
...   |
388 | |     Ok(())
389 | | }
    | |_^

thread &#39;rustc&#39; panicked at &#39;Box&lt;Any&gt;&#39;, /home/theodegioanni/github/rust/library/std/src/panic.rs:59:5
stack backtrace:
   0: std::panicking::begin_panic
             at ./library/std/src/panicking.rs:519:12
   1: std::panic::panic_any
             at ./library/std/src/panic.rs:59:5
   2: rustc_errors::HandlerInner::span_bug
             at ./compiler/rustc_errors/src/lib.rs:951:9
   3: rustc_errors::Handler::span_bug
             at ./compiler/rustc_errors/src/lib.rs:670:9
   4: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}
             at ./compiler/rustc_middle/src/util/bug.rs:33:40
   5: rustc_middle::ty::context::tls::with_opt::{{closure}}
             at ./compiler/rustc_middle/src/ty/context.rs:1800:40
   6: rustc_middle::ty::context::tls::with_context_opt
             at ./compiler/rustc_middle/src/ty/context.rs:1752:22
   7: rustc_middle::ty::context::tls::with_opt
             at ./compiler/rustc_middle/src/ty/context.rs:1800:9
   8: rustc_middle::util::bug::opt_span_bug_fmt
             at ./compiler/rustc_middle/src/util/bug.rs:30:5
   9: rustc_middle::util::bug::span_bug_fmt
             at ./compiler/rustc_middle/src/util/bug.rs:21:5
  10: rustc_codegen_ssa::mir::analyze::cleanup_kinds::propagate::{{closure}}
             at ./compiler/rustc_codegen_ssa/src/mir/analyze.rs:394:21
  11: rustc_codegen_ssa::mir::analyze::cleanup_kinds::propagate
             at ./compiler/rustc_codegen_ssa/src/mir/analyze.rs:439:29
  12: rustc_codegen_ssa::mir::analyze::cleanup_kinds
             at ./compiler/rustc_codegen_ssa/src/mir/analyze.rs:451:5
  13: rustc_codegen_ssa::mir::codegen_mir
             at ./compiler/rustc_codegen_ssa/src/mir/mod.rs:152:25
  14: rustc_codegen_ssa::base::codegen_instance
             at ./compiler/rustc_codegen_ssa/src/base.rs:342:5
  15: &lt;rustc_middle::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt&gt;::define
             at ./compiler/rustc_codegen_ssa/src/mono_item.rs:70:17
  16: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
             at ./compiler/rustc_codegen_llvm/src/base.rs:141:17
  17: rustc_query_system::dep_graph::graph::DepGraph&lt;K&gt;::with_task_impl
             at ./compiler/rustc_query_system/src/dep_graph/graph.rs:270:14
  18: rustc_query_system::dep_graph::graph::DepGraph&lt;K&gt;::with_task
             at ./compiler/rustc_query_system/src/dep_graph/graph.rs:205:9
  19: rustc_codegen_llvm::base::compile_codegen_unit
             at ./compiler/rustc_codegen_llvm/src/base.rs:117:9
  20: &lt;rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods&gt;::compile_codegen_unit
             at ./compiler/rustc_codegen_llvm/src/lib.rs:108:9
  21: rustc_codegen_ssa::base::codegen_crate
             at ./compiler/rustc_codegen_ssa/src/base.rs:646:38
  22: &lt;rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend&gt;::codegen_crate
             at ./compiler/rustc_codegen_llvm/src/lib.rs:265:18
  23: rustc_interface::passes::start_codegen::{{closure}}
             at ./compiler/rustc_interface/src/passes.rs:1017:9
  24: rustc_data_structures::profiling::VerboseTimingGuard::run
             at ./compiler/rustc_data_structures/src/profiling.rs:573:9
  25: rustc_session::utils::&lt;impl rustc_session::session::Session&gt;::time
             at ./compiler/rustc_session/src/utils.rs:16:9
  26: rustc_interface::passes::start_codegen
             at ./compiler/rustc_interface/src/passes.rs:1016:19
  27: rustc_interface::queries::Queries::ongoing_codegen::{{closure}}::{{closure}}
             at ./compiler/rustc_interface/src/queries.rs:296:20
  28: rustc_interface::passes::QueryContext::enter::{{closure}}
             at ./compiler/rustc_interface/src/passes.rs:750:42
  29: rustc_middle::ty::context::tls::enter_context::{{closure}}
             at ./compiler/rustc_middle/src/ty/context.rs:1735:50
  30: rustc_middle::ty::context::tls::set_tlv
             at ./compiler/rustc_middle/src/ty/context.rs:1719:9
  31: rustc_middle::ty::context::tls::enter_context
             at ./compiler/rustc_middle/src/ty/context.rs:1735:9
  32: rustc_interface::passes::QueryContext::enter
             at ./compiler/rustc_interface/src/passes.rs:750:9
  33: rustc_interface::queries::Queries::ongoing_codegen::{{closure}}
             at ./compiler/rustc_interface/src/queries.rs:287:13
  34: rustc_interface::queries::Query&lt;T&gt;::compute
             at ./compiler/rustc_interface/src/queries.rs:40:28
  35: rustc_interface::queries::Queries::ongoing_codegen
             at ./compiler/rustc_interface/src/queries.rs:285:9
  36: rustc_driver::run_compiler::{{closure}}::{{closure}}
             at ./compiler/rustc_driver/src/lib.rs:415:13
  37: rustc_interface::queries::&lt;impl rustc_interface::interface::Compiler&gt;::enter
             at ./compiler/rustc_interface/src/queries.rs:428:19
  38: rustc_driver::run_compiler::{{closure}}
             at ./compiler/rustc_driver/src/lib.rs:314:22
  39: rustc_interface::interface::create_compiler_and_run::{{closure}}
             at ./compiler/rustc_interface/src/interface.rs:208:13
  40: rustc_span::with_source_map
             at ./compiler/rustc_span/src/lib.rs:871:5
  41: rustc_interface::interface::create_compiler_and_run
             at ./compiler/rustc_interface/src/interface.rs:202:5
  42: rustc_interface::interface::run_compiler::{{closure}}
             at ./compiler/rustc_interface/src/interface.rs:224:12
  43: rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::{{closure}}::{{closure}}
             at ./compiler/rustc_interface/src/util.rs:155:13
  44: scoped_tls::ScopedKey&lt;T&gt;::set
             at /home/theodegioanni/.cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-1.0.0/src/lib.rs:137:9
  45: rustc_span::with_session_globals
             at ./compiler/rustc_span/src/lib.rs:105:5
  46: rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals::{{closure}}
             at ./compiler/rustc_interface/src/util.rs:153:9
  47: rustc_interface::util::scoped_thread::{{closure}}
             at ./compiler/rustc_interface/src/util.rs:128:24
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&amp;template=ice.md

note: rustc 1.54.0-dev running on x86_64-unknown-linux-gnu

note: compiler flags: -Z macro-backtrace -Z binary-dep-depinfo -Z force-unstable-if-unmarked -C opt-level=3 -C embed-bitcode=no -C debuginfo=1 -C debug-assertions=on -C overflow-checks=off -C link-args=-Wl,-rpath,$ORIGIN/../lib -C prefer-dynamic -C embed-bitcode=yes --crate-type dylib --crate-type rlib

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: aborting due to previous error

error: could not compile `test`

To learn more, run the command again with --verbose.
command did not execute successfully: &quot;/home/theodegioanni/github/rust/build/x86_64-unknown-linux-gnu/stage0/bin/cargo&quot; &quot;build&quot; &quot;--target&quot; &quot;x86_64-unknown-linux-gnu&quot; &quot;-Zbinary-dep-depinfo&quot; &quot;-j&quot; &quot;16&quot; &quot;--release&quot; &quot;--features&quot; &quot;panic-unwind backtrace compiler-builtins-c&quot; &quot;--manifest-path&quot; &quot;/home/theodegioanni/github/rust/library/test/Cargo.toml&quot; &quot;--message-format&quot; &quot;json-render-diagnostics&quot;
expected success, got: exit code: 101
failed to run: /home/theodegioanni/github/rust/build/bootstrap/debug/bootstrap test --bless src/test/mir-opt/separate_const_switch.rs
Build completed unsuccessfully in 0:00:04
</code></pre></div>



<a name="238921078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921078">(May 15 2021 at 19:48)</a>:</h4>
<p>here is the full error</p>



<a name="238921250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921250">(May 15 2021 at 19:51)</a>:</h4>
<p>"funclet" indicates that this is happening in the unwind path</p>



<a name="238921258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921258">(May 15 2021 at 19:51)</a>:</h4>
<p>do we have special requirements there to handle funclets, that your pass is maybe violating?</p>



<a name="238921354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921354">(May 15 2021 at 19:53)</a>:</h4>
<p>since validation isn't failing it's probably not something like jumping from non-cleanup to cleanup from a non-unwind edge</p>



<a name="238921661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921661">(May 15 2021 at 20:00)</a>:</h4>
<p>That's interesting. So far I've been avoiding to apply the pass to unwind jumps <em>just in case</em>. That it hit something there anyway is puzzling.</p>



<a name="238921750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921750">(May 15 2021 at 20:01)</a>:</h4>
<p>What I do is abort the optimization if the parent we try to redirect was going to the block from which we started through an unwind-related jump</p>



<a name="238921834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238921834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238921834">(May 15 2021 at 20:02)</a>:</h4>
<p>I'm going to push my draft code to my branch if you want to check it out</p>



<a name="238922117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238922117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238922117">(May 15 2021 at 20:07)</a>:</h4>
<p>oh well gotta love sweet git issues</p>



<a name="238922978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238922978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238922978">(May 15 2021 at 20:23)</a>:</h4>
<p><a href="https://github.com/Moxinilian/rust/blob/separate-const-switch/compiler/rustc_mir/src/transform/separate_const_switch.rs">https://github.com/Moxinilian/rust/blob/separate-const-switch/compiler/rustc_mir/src/transform/separate_const_switch.rs</a></p>



<a name="238922985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238922985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238922985">(May 15 2021 at 20:23)</a>:</h4>
<p>I had to recreate my repo from scratch, but here it is</p>



<a name="238923176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238923176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238923176">(May 15 2021 at 20:27)</a>:</h4>
<p>(btw <span class="user-mention" data-user-id="125270">@scottmcm</span> if you want to have a look at the code)</p>



<a name="238977055"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/238977055" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#238977055">(May 16 2021 at 13:14)</a>:</h4>
<p>I've narrowed down what I think caused the bug. I don't have enough battery to investigate further but I think it's on my side, specifically when redirecting call terminators.</p>



<a name="239373104"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239373104" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239373104">(May 19 2021 at 06:31)</a>:</h4>
<p>(still working on this, but only on week-ends currently)</p>



<a name="239781663"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239781663" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239781663">(May 21 2021 at 16:56)</a>:</h4>
<p>Is it necessary for unwind branches to have only one caller ever? (if so, why?)<br>
In that case, is there an easy way to copy a basic block that can unwind?</p>



<a name="239789668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239789668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239789668">(May 21 2021 at 17:58)</a>:</h4>
<p>I don't know the answer for unwind branches.</p>
<p>I'd be tempted to solve it by not duplicating a block that can unwind, because duplicating function call sites isn't always a good idea -- it can make a big difference for inlining, for example, where it's often much more profitable to inline a called-from-one-place function than a called-from-two-places one.</p>



<a name="239789841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239789841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239789841">(May 21 2021 at 17:59)</a>:</h4>
<p>Alright! I'm removing function calls then.<br>
Between silently giving up or panicking, which is better practice in case of a logic error in an optimization pass? Or is there something better?</p>



<a name="239790250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239790250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239790250">(May 21 2021 at 18:02)</a>:</h4>
<p>As in "there's a bug in the optimization pass because it encountered something unexpected"?</p>
<p>There are a bunch of macros for that like <code>span_bug!</code> that emit an ICE referencing the code.</p>



<a name="239790569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239790569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239790569">(May 21 2021 at 18:04)</a>:</h4>
<p>Here's an example of that: <a href="https://github.com/rust-lang/rust/pull/84811/files#diff-384affc1b4190940f114f3fcebbf969e7e18657a71ef9001da6b223a036687d9R162">https://github.com/rust-lang/rust/pull/84811/files#diff-384affc1b4190940f114f3fcebbf969e7e18657a71ef9001da6b223a036687d9R162</a></p>
<p>If it's not a bug, there's the logging macros if you want to record "hey, I wanted to do this but couldn't because of _____".</p>



<a name="239790834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239790834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239790834">(May 21 2021 at 18:06)</a>:</h4>
<p>I meant as a bug, like an unreachable reached</p>



<a name="239790863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239790863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239790863">(May 21 2021 at 18:06)</a>:</h4>
<p>So the ICE thing will be great, thanks!</p>



<a name="239791459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239791459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239791459">(May 21 2021 at 18:11)</a>:</h4>
<p>urg, it still fails on the "funclet has two parents things"</p>



<a name="239791468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239791468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239791468">(May 21 2021 at 18:11)</a>:</h4>
<p>i'll try being even more conservative</p>



<a name="239791482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239791482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239791482">(May 21 2021 at 18:12)</a>:</h4>
<p>not sure how but I'll try</p>



<a name="239791555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239791555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239791555">(May 21 2021 at 18:12)</a>:</h4>
<p>Aside: <a href="https://github.com/rust-lang/rust/issues/85365">#85365</a> got me even more excited for this.  I hadn't realized it would apply even for simple things like <code>.last().unwrap()</code> -- those seem way more likely in real code than the explicit match-of-match that <code>?</code> does.</p>



<a name="239791592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239791592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239791592">(May 21 2021 at 18:12)</a>:</h4>
<p>yeah same!</p>



<a name="239792275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239792275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239792275">(May 21 2021 at 18:18)</a>:</h4>
<p>reminder of what the bug is:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">error</span>: <span class="nc">internal</span><span class="w"> </span><span class="n">compiler</span><span class="w"> </span><span class="n">error</span>: <span class="nc">compiler</span><span class="o">/</span><span class="n">rustc_codegen_ssa</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">mir</span><span class="o">/</span><span class="n">analyze</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">394</span>:<span class="mi">21</span>: <span class="nc">funclet</span><span class="w"> </span><span class="n">bb235</span><span class="w"> </span><span class="n">has</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="n">parents</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">bb221</span><span class="w"> </span><span class="n">and</span><span class="w"> </span><span class="n">bb273</span><span class="w"></span>
<span class="w">   </span><span class="o">-</span>-&gt; <span class="nc">library</span><span class="o">/</span><span class="n">test</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">lib</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">201</span>:<span class="mi">1</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w"></span>
<span class="mi">201</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">run_tests</span><span class="o">&lt;</span><span class="n">F</span><span class="o">&gt;</span><span class="p">(</span><span class="w"></span>
<span class="mi">202</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="n">opts</span>: <span class="kp">&amp;</span><span class="nc">TestOpts</span><span class="p">,</span><span class="w"></span>
<span class="mi">203</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="n">tests</span>: <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">TestDescAndFn</span><span class="o">&gt;</span><span class="p">,</span><span class="w"></span>
<span class="mi">204</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="k">mut</span><span class="w"> </span><span class="n">notify_about_test_event</span>: <span class="nc">F</span><span class="p">,</span><span class="w"></span>
<span class="o">..</span><span class="p">.</span><span class="w">   </span><span class="o">|</span><span class="w"></span>
<span class="mi">388</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"></span>
<span class="mi">389</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="o">|</span><span class="w"> </span><span class="o">|</span><span class="n">_</span><span class="o">^</span><span class="w"></span>
</code></pre></div>



<a name="239792323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239792323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239792323">(May 21 2021 at 18:19)</a>:</h4>
<p>I'm going to push the new version of the optimization that covers fewer cases</p>



<a name="239792423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239792423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239792423">(May 21 2021 at 18:19)</a>:</h4>
<p><a href="https://github.com/Moxinilian/rust/blob/separate-const-switch/compiler/rustc_mir/src/transform/separate_const_switch.rs">https://github.com/Moxinilian/rust/blob/separate-const-switch/compiler/rustc_mir/src/transform/separate_const_switch.rs</a></p>



<a name="239792803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239792803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239792803">(May 21 2021 at 18:22)</a>:</h4>
<p>what the code does:</p>
<ul>
<li>find a block that ends in a switch</li>
<li>track if there is an unique place set before the current basic block that determines the result of the switch (this is not optimal  btw), this is the part that resolves switching over discriminants</li>
<li>if there is, iterate over the parents that have a reasonable terminator and find if the found determining place is likely to be (transitively) set from a const within that parent block</li>
<li>if so, add the corresponding edge to a vector of edges to duplicate</li>
<li>once this is done, iterate over the found edges: copy the target block and replace the reference to the target block in the origin block with the new block</li>
</ul>



<a name="239792893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239792893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239792893">(May 21 2021 at 18:23)</a>:</h4>
<p>(the double pass approach, while suboptimal in terms of generated code, makes the pass relatively fast because it only computes parents once and does not go down the recursion rabbit hole)</p>



<a name="239793189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793189">(May 21 2021 at 18:25)</a>:</h4>
<p>now as someone said earlier, the fact it is talking about funclets means we are talking about something in an unwing branch</p>



<a name="239793207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793207">(May 21 2021 at 18:25)</a>:</h4>
<p>which here does not make sense as I'm not even touching them</p>



<a name="239793225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793225">(May 21 2021 at 18:25)</a>:</h4>
<p>unless there is a switch within an unwind branch</p>



<a name="239793230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793230">(May 21 2021 at 18:26)</a>:</h4>
<p>that could be it</p>



<a name="239793279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793279">(May 21 2021 at 18:26)</a>:</h4>
<p>maybe I could just filter them out</p>



<a name="239793382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793382">(May 21 2021 at 18:27)</a>:</h4>
<p>oh right there's the <code>is_cleanup</code> field, it feels like it was made for that purpose</p>



<a name="239793634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239793634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239793634">(May 21 2021 at 18:28)</a>:</h4>
<p>besides unwinding should be a cold path, right</p>



<a name="239794103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239794103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239794103">(May 21 2021 at 18:32)</a>:</h4>
<p>it works!!</p>



<a name="239794491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239794491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239794491">(May 21 2021 at 18:35)</a>:</h4>
<p>I actually now need to make sure it does get optimized away down the line</p>



<a name="239796653"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239796653" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239796653">(May 21 2021 at 18:53)</a>:</h4>
<p>yep! it works somewhere down the line</p>



<a name="239796765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239796765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239796765">(May 21 2021 at 18:54)</a>:</h4>
<div class="codehilite"><pre><span></span><code>bb1: {
          StorageLive(_6);                 // scope 0 at $DIR/separate_const_switch.rs:18:17: 18:18
          _6 = move ((_1 as Err).0: E);    // scope 0 at $DIR/separate_const_switch.rs:18:17: 18:18
          StorageLive(_7);                 // scope 2 at $DIR/separate_const_switch.rs:18:42: 18:43
          _7 = move _6;                    // scope 2 at $DIR/separate_const_switch.rs:18:42: 18:43
          _12 = const true;                // scope 2 at $DIR/separate_const_switch.rs:18:23: 18:44
          _13 = const true;                // scope 2 at $DIR/separate_const_switch.rs:18:23: 18:44
          ((_2 as Break).0: E) = move _7;  // scope 2 at $DIR/separate_const_switch.rs:18:23: 18:44
          discriminant(_2) = 1;            // scope 2 at $DIR/separate_const_switch.rs:18:23: 18:44
          StorageDead(_7);                 // scope 2 at $DIR/separate_const_switch.rs:18:43: 18:44
          StorageDead(_6);                 // scope 0 at $DIR/separate_const_switch.rs:18:43: 18:44
          goto -&gt; bb3;                     // scope 0 at $DIR/separate_const_switch.rs:16:9: 19:10
      }

      bb2: {
          StorageLive(_4);                 // scope 0 at $DIR/separate_const_switch.rs:17:16: 17:17
          _4 = move ((_1 as Ok).0: T);     // scope 0 at $DIR/separate_const_switch.rs:17:16: 17:17
          StorageLive(_5);                 // scope 1 at $DIR/separate_const_switch.rs:17:44: 17:45
          _5 = move _4;                    // scope 1 at $DIR/separate_const_switch.rs:17:44: 17:45
          _12 = const true;                // scope 1 at $DIR/separate_const_switch.rs:17:22: 17:46
          _13 = const true;                // scope 1 at $DIR/separate_const_switch.rs:17:22: 17:46
          ((_2 as Continue).0: T) = move _5; // scope 1 at $DIR/separate_const_switch.rs:17:22: 17:46
          discriminant(_2) = 0;            // scope 1 at $DIR/separate_const_switch.rs:17:22: 17:46
          StorageDead(_5);                 // scope 1 at $DIR/separate_const_switch.rs:17:45: 17:46
          StorageDead(_4);                 // scope 0 at $DIR/separate_const_switch.rs:17:45: 17:46
          goto -&gt; bb3;                     // scope 0 at $DIR/separate_const_switch.rs:16:9: 19:10
      }

      bb3: {
          _8 = discriminant(_2);           // scope 0 at $DIR/separate_const_switch.rs:21:9: 21:33
          switchInt(move _8) -&gt; [0_isize: bb5, otherwise: bb4]; // scope 0 at $DIR/separate_const_switch.rs:21:9: 21:33
      }
</code></pre></div>



<a name="239796779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239796779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239796779">(May 21 2021 at 18:54)</a>:</h4>
<p>in the end becomes</p>



<a name="239796816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239796816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239796816">(May 21 2021 at 18:54)</a>:</h4>
<div class="codehilite"><pre><span></span><code>      bb1: {
          StorageLive(_6);                 // scope 0 at $DIR/separate_const_switch.rs:18:17: 18:18
          _6 = move ((_1 as Err).0: E);    // scope 0 at $DIR/separate_const_switch.rs:18:17: 18:18
          StorageLive(_7);                 // scope 2 at $DIR/separate_const_switch.rs:18:42: 18:43
          _7 = move _6;                    // scope 2 at $DIR/separate_const_switch.rs:18:42: 18:43
          ((_2 as Break).0: E) = move _7;  // scope 2 at $DIR/separate_const_switch.rs:18:23: 18:44
          discriminant(_2) = 1;            // scope 2 at $DIR/separate_const_switch.rs:18:23: 18:44
          StorageDead(_7);                 // scope 2 at $DIR/separate_const_switch.rs:18:43: 18:44
          StorageDead(_6);                 // scope 0 at $DIR/separate_const_switch.rs:18:43: 18:44
          _8 = discriminant(_2);           // scope 0 at $DIR/separate_const_switch.rs:21:9: 21:33
          switchInt(move _8) -&gt; [0_isize: bb4, otherwise: bb3]; // scope 0 at $DIR/separate_const_switch.rs:21:9: 21:33
      }

      bb2: {
          StorageLive(_4);                 // scope 0 at $DIR/separate_const_switch.rs:17:16: 17:17
          _4 = move ((_1 as Ok).0: T);     // scope 0 at $DIR/separate_const_switch.rs:17:16: 17:17
          StorageLive(_5);                 // scope 1 at $DIR/separate_const_switch.rs:17:44: 17:45
          _5 = move _4;                    // scope 1 at $DIR/separate_const_switch.rs:17:44: 17:45
          ((_2 as Continue).0: T) = move _5; // scope 1 at $DIR/separate_const_switch.rs:17:22: 17:46
          discriminant(_2) = 0;            // scope 1 at $DIR/separate_const_switch.rs:17:22: 17:46
          StorageDead(_5);                 // scope 1 at $DIR/separate_const_switch.rs:17:45: 17:46
          StorageDead(_4);                 // scope 0 at $DIR/separate_const_switch.rs:17:45: 17:46
          _8 = discriminant(_2);           // scope 0 at $DIR/separate_const_switch.rs:21:9: 21:33
          switchInt(move _8) -&gt; [0_isize: bb4, otherwise: bb3]; // scope 0 at $DIR/separate_const_switch.rs:21:9: 21:33
      }
</code></pre></div>



<a name="239796942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239796942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239796942">(May 21 2021 at 18:55)</a>:</h4>
<p>the switchs seem to not disappear but the propagation is so trivial that I hope LLVM can pick it up</p>



<a name="239797224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797224">(May 21 2021 at 18:57)</a>:</h4>
<p><span class="user-mention" data-user-id="125270">@scottmcm</span></p>



<a name="239797283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797283">(May 21 2021 at 18:58)</a>:</h4>
<p>I'm going to clean up the code and make it more elegant in places now</p>



<a name="239797395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797395">(May 21 2021 at 18:59)</a>:</h4>
<p>If you can run this pass before <code>const_prop</code> it should pick that up and turn the <code>move _8</code>s into <code>const 0</code> or <code>const 1</code> as appropriate. <code>simplify_branches</code> will then remove the <code>switchInts</code>.</p>



<a name="239797554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797554">(May 21 2021 at 19:00)</a>:</h4>
<p>Interestingly I put it just before const prop, and yet it did not pick it up</p>



<a name="239797624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797624">(May 21 2021 at 19:00)</a>:</h4>
<p>maybe it's because there is still a bunch of indirection</p>



<a name="239797720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797720">(May 21 2021 at 19:01)</a>:</h4>
<p>once a test I'm doing is done compiling i'll try moving the pass to other places</p>



<a name="239797727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797727">(May 21 2021 at 19:01)</a>:</h4>
<p>Hmm ... <code>const_prop</code> is pretty conservative if the local is assigned to more than once so that might be why it's not picking that up.</p>



<a name="239797756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797756">(May 21 2021 at 19:01)</a>:</h4>
<p>that's something SSA would solve I bet</p>



<a name="239797826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797826">(May 21 2021 at 19:01)</a>:</h4>
<p>Yeah, there's also an issue about rewriting the pass to use a dataflow analysis which would probably also resolve this issue.</p>



<a name="239797887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797887">(May 21 2021 at 19:02)</a>:</h4>
<p>very interesting</p>



<a name="239797904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797904">(May 21 2021 at 19:02)</a>:</h4>
<p>I'll look that issue up</p>



<a name="239797936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239797936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239797936">(May 21 2021 at 19:02)</a>:</h4>
<p>I find optimizing control flow graphs super entertaining</p>



<a name="239798144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239798144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239798144">(May 21 2021 at 19:03)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/80038">#80038</a> :)</p>



<a name="239798245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239798245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239798245">(May 21 2021 at 19:04)</a>:</h4>
<p>thank you!</p>



<a name="239798297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239798297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239798297">(May 21 2021 at 19:04)</a>:</h4>
<p>You're welcome!</p>



<a name="239798728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239798728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239798728">(May 21 2021 at 19:07)</a>:</h4>
<p>Looking great <span class="user-mention" data-user-id="409735">@Théo Degioanni</span>!  I'm also a bit surprised that mir-opt const prop isn't picking it up, but I agree that LLVM should have no problem with it.</p>



<a name="239816468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239816468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239816468">(May 21 2021 at 21:42)</a>:</h4>
<p>The code is now ready for a PR. Just before doing that I'll look at some generated assembly to brag in the PR description.</p>



<a name="239816527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239816527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239816527">(May 21 2021 at 21:43)</a>:</h4>
<p>(for real I want to make sure there definitely is an assembly-level win here)</p>



<a name="239819176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239819176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239819176">(May 21 2021 at 22:10)</a>:</h4>
<p>What opt level should I restrict the pass to? I don't exactly know what each value mean</p>



<a name="239819764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239819764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239819764">(May 21 2021 at 22:17)</a>:</h4>
<p>hmmm</p>



<a name="239819781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239819781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239819781">(May 21 2021 at 22:17)</a>:</h4>
<p>I tried it with this example</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">demo</span><span class="p">(</span><span class="n">x</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="o">?</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="239819873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239819873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239819873">(May 21 2021 at 22:18)</a>:</h4>
<p>before:</p>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code><span class="nl">demo:</span>
    <span class="na">.cfi_startproc</span>
    <span class="nf">xorl</span>    <span class="nv">%ecx</span><span class="p">,</span> <span class="nv">%ecx</span>
    <span class="nf">testl</span>   <span class="nv">%edi</span><span class="p">,</span> <span class="nv">%edi</span>
    <span class="nf">setne</span>   <span class="nv">%cl</span>
    <span class="nf">movabsq</span> <span class="no">$-4294967296</span><span class="p">,</span> <span class="nv">%rax</span>
    <span class="nf">andq</span>    <span class="nv">%rdi</span><span class="p">,</span> <span class="nv">%rax</span>
    <span class="nf">orq</span> <span class="nv">%rcx</span><span class="p">,</span> <span class="nv">%rax</span>
    <span class="nf">retq</span>
</code></pre></div>
<p>after:</p>
<div class="codehilite" data-code-language="GAS"><pre><span></span><code><span class="nl">demo:</span>
    <span class="na">.cfi_startproc</span>
    <span class="nf">movabsq</span> <span class="no">$-4294967296</span><span class="p">,</span> <span class="nv">%rax</span>
    <span class="nf">andq</span>    <span class="nv">%rdi</span><span class="p">,</span> <span class="nv">%rax</span>
    <span class="nf">testl</span>   <span class="nv">%edi</span><span class="p">,</span> <span class="nv">%edi</span>
    <span class="nf">je</span>  <span class="no">.LBB5_1</span>
    <span class="nf">orq</span> <span class="no">$1</span><span class="p">,</span> <span class="nv">%rax</span>
    <span class="nf">retq</span>
<span class="nl">.LBB5_1:</span>
    <span class="nf">retq</span>
</code></pre></div>



<a name="239819882"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239819882" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239819882">(May 21 2021 at 22:18)</a>:</h4>
<p>isn't that jump bad?</p>



<a name="239821636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239821636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239821636">(May 21 2021 at 22:38)</a>:</h4>
<p>ah I think it's because it doesn't inline it right</p>



<a name="239821681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239821681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239821681">(May 21 2021 at 22:39)</a>:</h4>
<p>I don't know how to make it do that however<br>
Anyway, good night</p>



<a name="239826592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239826592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239826592">(May 21 2021 at 23:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409735">Théo Degioanni</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/239819176">said</a>:</p>
<blockquote>
<p>What opt level should I restrict the pass to? I don't exactly know what each value mean</p>
</blockquote>
<p>For the first PR it'd probably be easiest to put it at some level that doesn't run by default -- that way you can get it checked in with tests and such, but don't have to worry about any potential compiler perf impact.</p>



<a name="239826746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239826746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239826746">(May 21 2021 at 23:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409735">Théo Degioanni</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/239821636">said</a>:</p>
<blockquote>
<p>ah I think it's because it doesn't inline it right</p>
</blockquote>
<p><span class="user-mention" data-user-id="307537">@Noah Lev</span> has been experimenting with <code>#[inline(always)]</code> on <code>branch</code> to try to get past that, see <a href="https://github.com/rust-lang/rust/pull/85523">https://github.com/rust-lang/rust/pull/85523</a></p>
<p>You could perhaps put that in <code>core</code> yourself and see if you get better optimization results.</p>



<a name="239846220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239846220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239846220">(May 22 2021 at 05:26)</a>:</h4>
<p>Okay. I'll also try looking at what has been said in the Vec::pop issue.</p>



<a name="239847997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239847997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239847997">(May 22 2021 at 06:01)</a>:</h4>
<p>Hmm in fact it does inline it properly. I fear LLVM does not understand that the two paths are actually no-ops.</p>



<a name="239848129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239848129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239848129">(May 22 2021 at 06:03)</a>:</h4>
<p>That would make sense if it did not actually propagate the switch. Later today I'll try emitting post-opt LLVM IR to see if that is the case.</p>



<a name="239848395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239848395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239848395">(May 22 2021 at 06:06)</a>:</h4>
<p>With a better ConstProp it really would get propagated into an (almost?) identity at the MIR level so I'm not super worried, but for the time being this is weird.</p>



<a name="239848675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239848675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239848675">(May 22 2021 at 06:11)</a>:</h4>
<p>Interestingly, I think it woudl also have to realize that when matching initialially, as it outputs</p>
<div class="codehilite"><pre><span></span><code>switchInt(move _3) -&gt; [0_isize: bb2, otherwise: bb1];
</code></pre></div>
<p>it will have to know what otherwise can only be 1 for it to understand  it can assume no change. If so, maybe we'd have to tweak the switch emission so it is a 0: bb2, 1: bb1 and otherwise: unreachable</p>



<a name="239850034"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239850034" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239850034">(May 22 2021 at 06:34)</a>:</h4>
<p>Oh, curious.  Usually the MIR does have the 0/1/unreachable kind of switching in its output.  (LLVM likes changing that back to 0/otherwise, annoyingly, but MIR usually keeps it, I'd thought.)</p>



<a name="239854533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239854533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239854533">(May 22 2021 at 07:58)</a>:</h4>
<p>Yeah okay this is the LLVM IR that I think rustc generates:</p>
<div class="codehilite" data-code-language="LLVM-MIR"><pre><span></span><code>; Function Attrs: norecurse nounwind nonlazybind readnone uwtable willreturn
define dso_local i64 @demo(i64 %0) unnamed_addr <span class="c">#4 {</span>
start:
  %_9 = and i64 %0, 4294967295
  %switch = icmp eq i64 %_9, 0
  %self.sroa.5.0.extract.shift = and i64 %0, -4294967296
  br i1 %switch, label %bb1, label %bb2

bb1:                                              ; preds = %start
  ret i64 %self.sroa.5.0.extract.shift

bb2:                                              ; preds = %start
  %.sroa.0.0.insert.insert = or i64 %self.sroa.5.0.extract.shift, 1
  ret i64 %.sroa.0.0.insert.insert
}
</code></pre></div>



<a name="239854553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239854553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239854553">(May 22 2021 at 07:58)</a>:</h4>
<p>it's understandable it cannot understand what is going on</p>



<a name="239854776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239854776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239854776">(May 22 2021 at 08:01)</a>:</h4>
<p>ah no that's after LLVM opt I think</p>



<a name="239856262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239856262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239856262">(May 22 2021 at 08:27)</a>:</h4>
<p>okay so rustc does emit the 0, 1, otherwise information in IR actually</p>



<a name="239856303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239856303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239856303">(May 22 2021 at 08:28)</a>:</h4>
<p>but it seems like LLVM still can't figure it out</p>



<a name="239856320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239856320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239856320">(May 22 2021 at 08:28)</a>:</h4>
<p>yet it should know 0 and 1 are the only possible cases because the otherwise branch is indeed marked unreachable</p>



<a name="239891792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239891792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239891792">(May 22 2021 at 18:40)</a>:</h4>
<p><a href="/user_uploads/4715/SQ0TZ_uLXtWN9k0Poh3a_pE2/demo.pdf">demo.pdf</a></p>



<a name="239891809"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239891809" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239891809">(May 22 2021 at 18:40)</a>:</h4>
<p>this is the llvm cfg of the function before LLVM passes</p>



<a name="239892637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239892637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239892637">(May 22 2021 at 18:54)</a>:</h4>
<p>What is the MIR output on your example?</p>



<a name="239892903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239892903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239892903">(May 22 2021 at 19:00)</a>:</h4>
<p>for</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">demo</span><span class="p">(</span><span class="n">x</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">u32</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="o">?</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>the mir output is</p>
<div class="codehilite"><pre><span></span><code>fn demo(_1: Result&lt;i32, u32&gt;) -&gt; Result&lt;i32, u32&gt; {
    debug x =&gt; _1;                       // in scope 0 at test_opt.rs:6:13: 6:14
    let mut _0: std::result::Result&lt;i32, u32&gt;; // return place in scope 0 at test_opt.rs:6:37: 6:53
    let mut _2: i32;                     // in scope 0 at test_opt.rs:7:8: 7:10
    let mut _3: std::ops::ControlFlow&lt;std::result::Result&lt;std::convert::Infallible, u32&gt;, i32&gt;; // in scope 0 at test_opt.rs:7:8: 7:10
    let mut _4: std::result::Result&lt;i32, u32&gt;; // in scope 0 at test_opt.rs:7:8: 7:9
    let mut _5: isize;                   // in scope 0 at test_opt.rs:7:9: 7:10
    let _6: std::result::Result&lt;std::convert::Infallible, u32&gt;; // in scope 0 at test_opt.rs:7:9: 7:10
    let mut _7: std::result::Result&lt;std::convert::Infallible, u32&gt;; // in scope 0 at test_opt.rs:7:9: 7:10
    let _8: i32;                         // in scope 0 at test_opt.rs:7:8: 7:10
    scope 1 {
        debug residual =&gt; _6;            // in scope 1 at test_opt.rs:7:9: 7:10
        scope 2 {
            scope 8 (inlined &lt;Result&lt;i32, u32&gt; as FromResidual&lt;Result&lt;Infallible, u32&gt;&gt;&gt;::from_residual) { // at test_opt.rs:7:8: 7:10
                debug residual =&gt; _7;    // in scope 8 at test_opt.rs:7:8: 7:10
                let _15: u32;            // in scope 8 at test_opt.rs:7:8: 7:10
                let mut _16: u32;        // in scope 8 at test_opt.rs:7:8: 7:10
                let mut _17: u32;        // in scope 8 at test_opt.rs:7:8: 7:10
                scope 9 {
                    debug e =&gt; _15;      // in scope 9 at test_opt.rs:7:8: 7:10
                    scope 10 (inlined &lt;u32 as From&lt;u32&gt;&gt;::from) { // at test_opt.rs:7:8: 7:10
                        debug t =&gt; _17;  // in scope 10 at test_opt.rs:7:8: 7:10
                    }
                }
            }
        }
    }
    scope 3 {
        debug val =&gt; _8;                 // in scope 3 at test_opt.rs:7:8: 7:10
        scope 4 {
        }
    }
    scope 5 (inlined &lt;Result&lt;i32, u32&gt; as Try&gt;::branch) { // at test_opt.rs:7:8: 7:10
        debug self =&gt; _4;                // in scope 5 at test_opt.rs:7:8: 7:10
        let mut _9: isize;               // in scope 5 at test_opt.rs:7:8: 7:10
        let _10: i32;                    // in scope 5 at test_opt.rs:7:8: 7:10
        let mut _11: i32;                // in scope 5 at test_opt.rs:7:8: 7:10
        let _12: u32;                    // in scope 5 at test_opt.rs:7:8: 7:10
        let mut _13: std::result::Result&lt;std::convert::Infallible, u32&gt;; // in scope 5 at test_opt.rs:7:8: 7:10
        let mut _14: u32;                // in scope 5 at test_opt.rs:7:8: 7:10
        scope 6 {
            debug v =&gt; _10;              // in scope 6 at test_opt.rs:7:8: 7:10
        }
        scope 7 {
            debug e =&gt; _12;              // in scope 7 at test_opt.rs:7:8: 7:10
        }
    }

    bb0: {
        StorageLive(_2);                 // scope 0 at test_opt.rs:7:8: 7:10
        StorageLive(_3);                 // scope 0 at test_opt.rs:7:8: 7:10
        StorageLive(_4);                 // scope 0 at test_opt.rs:7:8: 7:9
        _4 = _1;                         // scope 0 at test_opt.rs:7:8: 7:9
        StorageLive(_9);                 // scope 0 at test_opt.rs:7:8: 7:10
        _9 = discriminant(_4);           // scope 5 at test_opt.rs:7:8: 7:10
        switchInt(move _9) -&gt; [0_isize: bb5, 1_isize: bb3, otherwise: bb4]; // scope 5 at test_opt.rs:7:8: 7:10
    }

    bb1: {
        StorageLive(_8);                 // scope 0 at test_opt.rs:7:8: 7:10
        _8 = ((_3 as Continue).0: i32);  // scope 0 at test_opt.rs:7:8: 7:10
        _2 = _8;                         // scope 4 at test_opt.rs:7:8: 7:10
        StorageDead(_8);                 // scope 0 at test_opt.rs:7:9: 7:10
        ((_0 as Ok).0: i32) = move _2;   // scope 0 at test_opt.rs:7:5: 7:11
        discriminant(_0) = 0;            // scope 0 at test_opt.rs:7:5: 7:11
        StorageDead(_2);                 // scope 0 at test_opt.rs:7:10: 7:11
        StorageDead(_3);                 // scope 0 at test_opt.rs:8:1: 8:2
        return;                          // scope 0 at test_opt.rs:8:2: 8:2
    }

    bb2: {
        StorageLive(_6);                 // scope 0 at test_opt.rs:7:9: 7:10
        _6 = ((_3 as Break).0: std::result::Result&lt;std::convert::Infallible, u32&gt;); // scope 0 at test_opt.rs:7:9: 7:10
        StorageLive(_7);                 // scope 2 at test_opt.rs:7:9: 7:10
        _7 = _6;                         // scope 2 at test_opt.rs:7:9: 7:10
        StorageLive(_15);                // scope 8 at test_opt.rs:7:8: 7:10
        _15 = move ((_7 as Err).0: u32); // scope 8 at test_opt.rs:7:8: 7:10
        StorageLive(_16);                // scope 9 at test_opt.rs:7:8: 7:10
        StorageLive(_17);                // scope 9 at test_opt.rs:7:8: 7:10
        _17 = move _15;                  // scope 9 at test_opt.rs:7:8: 7:10
        _16 = move _17;                  // scope 10 at test_opt.rs:7:8: 7:10
        StorageDead(_17);                // scope 9 at test_opt.rs:7:8: 7:10
        ((_0 as Err).0: u32) = move _16; // scope 9 at test_opt.rs:7:8: 7:10
        discriminant(_0) = 1;            // scope 9 at test_opt.rs:7:8: 7:10
        StorageDead(_16);                // scope 9 at test_opt.rs:7:8: 7:10
        StorageDead(_15);                // scope 8 at test_opt.rs:7:8: 7:10
        StorageDead(_7);                 // scope 2 at test_opt.rs:7:9: 7:10
        StorageDead(_6);                 // scope 0 at test_opt.rs:7:9: 7:10
        StorageDead(_2);                 // scope 0 at test_opt.rs:7:10: 7:11
        StorageDead(_3);                 // scope 0 at test_opt.rs:8:1: 8:2
        return;                          // scope 0 at test_opt.rs:8:2: 8:2
    }

    bb3: {
        StorageLive(_12);                // scope 5 at test_opt.rs:7:8: 7:10
        _12 = move ((_4 as Err).0: u32); // scope 5 at test_opt.rs:7:8: 7:10
        StorageLive(_13);                // scope 7 at test_opt.rs:7:8: 7:10
        StorageLive(_14);                // scope 7 at test_opt.rs:7:8: 7:10
        _14 = move _12;                  // scope 7 at test_opt.rs:7:8: 7:10
        ((_13 as Err).0: u32) = move _14; // scope 7 at test_opt.rs:7:8: 7:10
        discriminant(_13) = 1;           // scope 7 at test_opt.rs:7:8: 7:10
        StorageDead(_14);                // scope 7 at test_opt.rs:7:8: 7:10
        ((_3 as Break).0: std::result::Result&lt;std::convert::Infallible, u32&gt;) = move _13; // scope 7 at test_opt.rs:7:8: 7:10
        discriminant(_3) = 1;            // scope 7 at test_opt.rs:7:8: 7:10
        StorageDead(_13);                // scope 7 at test_opt.rs:7:8: 7:10
        StorageDead(_12);                // scope 5 at test_opt.rs:7:8: 7:10
        StorageDead(_9);                 // scope 0 at test_opt.rs:7:8: 7:10
        StorageDead(_4);                 // scope 0 at test_opt.rs:7:9: 7:10
        _5 = discriminant(_3);           // scope 0 at test_opt.rs:7:9: 7:10
        switchInt(move _5) -&gt; [0_isize: bb1, otherwise: bb2]; // scope 0 at test_opt.rs:7:9: 7:10
    }

    bb4: {
        unreachable;                     // scope 5 at test_opt.rs:7:8: 7:10
    }

    bb5: {
        StorageLive(_10);                // scope 5 at test_opt.rs:7:8: 7:10
        _10 = move ((_4 as Ok).0: i32);  // scope 5 at test_opt.rs:7:8: 7:10
        StorageLive(_11);                // scope 6 at test_opt.rs:7:8: 7:10
        _11 = move _10;                  // scope 6 at test_opt.rs:7:8: 7:10
        ((_3 as Continue).0: i32) = move _11; // scope 6 at test_opt.rs:7:8: 7:10
        discriminant(_3) = 0;            // scope 6 at test_opt.rs:7:8: 7:10
        StorageDead(_11);                // scope 6 at test_opt.rs:7:8: 7:10
        StorageDead(_10);                // scope 5 at test_opt.rs:7:8: 7:10
        StorageDead(_9);                 // scope 0 at test_opt.rs:7:8: 7:10
        StorageDead(_4);                 // scope 0 at test_opt.rs:7:9: 7:10
        _5 = discriminant(_3);           // scope 0 at test_opt.rs:7:9: 7:10
        switchInt(move _5) -&gt; [0_isize: bb1, otherwise: bb2]; // scope 0 at test_opt.rs:7:9: 7:10
    }
}
</code></pre></div>



<a name="239893000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893000">(May 22 2021 at 19:00)</a>:</h4>
<p>ConstProp does not pick up the constants so we still have switches in the end, and I wonder if that doesn't make LLVM miss it</p>



<a name="239893061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893061">(May 22 2021 at 19:01)</a>:</h4>
<p><a href="/user_uploads/4715/XvliVTgsC68inb-rEP58g72c/demo_after_llvm.pdf">demo_after_llvm.pdf</a></p>



<a name="239893063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893063">(May 22 2021 at 19:02)</a>:</h4>
<p>this is the final output out of LLVM</p>



<a name="239893447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893447">(May 22 2021 at 19:07)</a>:</h4>
<p>My understanding is that LLVM does not understand that it is extracting the enum tag, just to put it back in its result.</p>



<a name="239893459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893459">(May 22 2021 at 19:07)</a>:</h4>
<p>yeah that's correct</p>



<a name="239893470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893470">(May 22 2021 at 19:07)</a>:</h4>
<p>I don't really know what to do to make it realize that</p>



<a name="239893701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893701">(May 22 2021 at 19:11)</a>:</h4>
<p>the fact that one of the variables is called %switch makes me believe it incorrectly removes the switches ConstProp failed to simplify</p>



<a name="239893711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893711">(May 22 2021 at 19:11)</a>:</h4>
<p>The issues is that LLVM has no way to know that %switch is either 0 or 1.</p>



<a name="239893754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893754">(May 22 2021 at 19:12)</a>:</h4>
<p>Sorry, %_9.</p>



<a name="239893774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893774">(May 22 2021 at 19:12)</a>:</h4>
<p>I don't know if %_9 maps to the value being switched over in the first MIR switch or the ones being switched over in the unsimplified MIR switches</p>



<a name="239893795"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893795" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893795">(May 22 2021 at 19:13)</a>:</h4>
<p>the thing is, when you look at the IR before LLVM opt, it does have all the information</p>



<a name="239893814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893814">(May 22 2021 at 19:13)</a>:</h4>
<p>the unsimplified switches are const, and the first switch has three branches: 0 -&gt; somewhere, 1 -&gt; somewhere else, otherwise -&gt; unreachable</p>



<a name="239893877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239893877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239893877">(May 22 2021 at 19:14)</a>:</h4>
<p>maybe something to report upstream?</p>



<a name="239894130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239894130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239894130">(May 22 2021 at 19:18)</a>:</h4>
<p>I'd love to be able to print CFG graphs after each LLVM pass to see how the code evolves, but there seems to be no easy way to achieve that unfortunately</p>



<a name="239894138"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239894138" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239894138">(May 22 2021 at 19:19)</a>:</h4>
<p>I can try creating a hacky script tho</p>



<a name="239895718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239895718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239895718">(May 22 2021 at 19:46)</a>:</h4>
<p><a href="/user_uploads/4715/slYaE-ndvQcpPfdFRwcfM3OX/3.pdf">3.pdf</a></p>



<a name="239895748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239895748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239895748">(May 22 2021 at 19:47)</a>:</h4>
<p>interestingly, the 3rd thing it does to the IR is to get rid of the fact that it is only 0 or 1</p>



<a name="239895789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239895789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239895789">(May 22 2021 at 19:47)</a>:</h4>
<p>(this pdf file is the state of the IR after the 3rd pass)</p>



<a name="239896639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896639">(May 22 2021 at 20:02)</a>:</h4>
<p>I believe the culprit to be a pass called "Simplify the CFG"</p>



<a name="239896792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896792">(May 22 2021 at 20:05)</a>:</h4>
<p><a href="https://llvm.org/doxygen/SimplifyCFGPass_8cpp_source.html">https://llvm.org/doxygen/SimplifyCFGPass_8cpp_source.html</a></p>



<a name="239896794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896794">(May 22 2021 at 20:05)</a>:</h4>
<p>Did you push your code on github?</p>



<a name="239896820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896820">(May 22 2021 at 20:05)</a>:</h4>
<p>I'd like to understand why MIR const_prop does not fire.</p>



<a name="239896825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896825">(May 22 2021 at 20:05)</a>:</h4>
<p>my pass is up to date on my branch, one sec</p>



<a name="239896874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896874">(May 22 2021 at 20:06)</a>:</h4>
<p><a href="https://github.com/Moxinilian/rust/blob/separate-const-switch/compiler/rustc_mir/src/transform/separate_const_switch.rs">https://github.com/Moxinilian/rust/blob/separate-const-switch/compiler/rustc_mir/src/transform/separate_const_switch.rs</a></p>



<a name="239896893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896893">(May 22 2021 at 20:06)</a>:</h4>
<p>someone suggested ConstProp does not pick it up because the places it uses are assigned to twice</p>



<a name="239896898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239896898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239896898">(May 22 2021 at 20:06)</a>:</h4>
<p>which can be solved by the SSA suggestion, if that is the case</p>



<a name="239897125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239897125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239897125">(May 22 2021 at 20:11)</a>:</h4>
<p>Is the question about why does const-prop does not propagate a write of a discriminant, to a subsequent read?</p>



<a name="239897201"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239897201" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239897201">(May 22 2021 at 20:12)</a>:</h4>
<p>A  whole local would need to be a const for that to happen.</p>



<a name="239897205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239897205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239897205">(May 22 2021 at 20:12)</a>:</h4>
<p>I wonder if an even simpler peephole copy prop pass could make sense.</p>
<p>Like turning <code>discriminant(X) = Y; Z = discriminant(X);</code> into <code>discriminant(X) = Y; Z = Y;</code>.</p>



<a name="239897341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239897341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239897341">(May 22 2021 at 20:15)</a>:</h4>
<p>I know there's all the NRVO work going on, but that's far more complex than is needed for the case this is hitting...</p>



<a name="239897831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239897831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239897831">(May 22 2021 at 20:24)</a>:</h4>
<p>Running SimplifyCfg after the separate_const_switch passes allows ConstProp to pick up the optimization.</p>



<a name="239897963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239897963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239897963">(May 22 2021 at 20:27)</a>:</h4>
<div class="codehilite"><pre><span></span><code>// MIR for `demo` after PreCodegen

fn demo(_1: Result&lt;i32, u32&gt;) -&gt; Result&lt;i32, u32&gt; {
    debug x =&gt; _1;                       // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:8:9: 8:10
    let mut _0: std::result::Result&lt;i32, u32&gt;; // return place in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:8:33: 8:49
    let mut _2: i32;                     // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
    let mut _3: std::ops::ControlFlow&lt;std::result::Result&lt;std::convert::Infallible, u32&gt;, i32&gt;; // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
    let mut _4: std::result::Result&lt;i32, u32&gt;; // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:9
    let _5: std::result::Result&lt;std::convert::Infallible, u32&gt;; // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
    let mut _6: std::result::Result&lt;std::convert::Infallible, u32&gt;; // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
    let _7: i32;                         // in scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
    scope 1 {
        debug residual =&gt; _5;            // in scope 1 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        scope 2 {
            scope 8 (inlined &lt;Result&lt;i32, u32&gt; as FromResidual&lt;Result&lt;Infallible, u32&gt;&gt;&gt;::from_residual) { // at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                debug residual =&gt; _6;    // in scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                let _14: u32;            // in scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                let mut _15: u32;        // in scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                let mut _16: u32;        // in scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                scope 9 {
                    debug e =&gt; _14;      // in scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                    scope 10 (inlined &lt;u32 as From&lt;u32&gt;&gt;::from) { // at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                        debug t =&gt; _16;  // in scope 10 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
                    }
                }
            }
        }
    }
    scope 3 {
        debug val =&gt; _7;                 // in scope 3 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        scope 4 {
        }
    }
    scope 5 (inlined &lt;Result&lt;i32, u32&gt; as Try&gt;::branch) { // at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        debug self =&gt; _4;                // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        let mut _8: isize;               // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        let _9: i32;                     // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        let mut _10: i32;                // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        let _11: u32;                    // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        let mut _12: std::result::Result&lt;std::convert::Infallible, u32&gt;; // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        let mut _13: u32;                // in scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        scope 6 {
            debug v =&gt; _9;               // in scope 6 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        }
        scope 7 {
            debug e =&gt; _11;              // in scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        }
    }

    bb0: {
        StorageLive(_2);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_3);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_4);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:9
        _4 = _1;                         // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:9
        StorageLive(_8);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _8 = discriminant(_4);           // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        switchInt(move _8) -&gt; [0_isize: bb3, 1_isize: bb1, otherwise: bb2]; // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
    }

    bb1: {
        StorageLive(_11);                // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _11 = move ((_4 as Err).0: u32); // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_12);                // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_13);                // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _13 = move _11;                  // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        ((_12 as Err).0: u32) = move _13; // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        discriminant(_12) = 1;           // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_13);                // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        ((_3 as Break).0: std::result::Result&lt;std::convert::Infallible, u32&gt;) = move _12; // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        discriminant(_3) = 1;            // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_12);                // scope 7 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_11);                // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_8);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_4);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        StorageLive(_5);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        _5 = ((_3 as Break).0: std::result::Result&lt;std::convert::Infallible, u32&gt;); // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        StorageLive(_6);                 // scope 2 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        _6 = _5;                         // scope 2 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        StorageLive(_14);                // scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _14 = move ((_6 as Err).0: u32); // scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_15);                // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_16);                // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _16 = move _14;                  // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _15 = move _16;                  // scope 10 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_16);                // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        ((_0 as Err).0: u32) = move _15; // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        discriminant(_0) = 1;            // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_15);                // scope 9 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_14);                // scope 8 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_6);                 // scope 2 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        StorageDead(_5);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        StorageDead(_2);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:10: 9:11
        StorageDead(_3);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:10:1: 10:2
        return;                          // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:10:2: 10:2
    }
</code></pre></div>



<a name="239898054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239898054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239898054">(May 22 2021 at 20:29)</a>:</h4>
<div class="codehilite"><pre><span></span><code> bb2: {
        unreachable;                     // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
    }

    bb3: {
        StorageLive(_9);                 // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _9 = move ((_4 as Ok).0: i32);   // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageLive(_10);                // scope 6 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _10 = move _9;                   // scope 6 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        ((_3 as Continue).0: i32) = move _10; // scope 6 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        discriminant(_3) = 0;            // scope 6 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_10);                // scope 6 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_9);                 // scope 5 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_8);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_4);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        StorageLive(_7);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _7 = ((_3 as Continue).0: i32);  // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        _2 = _7;                         // scope 4 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:8: 9:10
        StorageDead(_7);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:9: 9:10
        ((_0 as Ok).0: i32) = move _2;   // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:5: 9:11
        discriminant(_0) = 0;            // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:5: 9:11
        StorageDead(_2);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:9:10: 9:11
        StorageDead(_3);                 // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:10:1: 10:2
        return;                          // scope 0 at /home/k1000/src/rust/rust/src/test/mir-opt/separate_const_switch.rs:10:2: 10:2
    }
}
</code></pre></div>



<a name="239898313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239898313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239898313">(May 22 2021 at 20:33)</a>:</h4>
<p>However, the final LLVM IR is not modified.</p>



<a name="239898858"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239898858" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239898858">(May 22 2021 at 20:43)</a>:</h4>
<p>Yeah I think it’s an oversight in LLVM. For some reason it does not reason about unreachable blocks at all.</p>



<a name="239899183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239899183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239899183">(May 22 2021 at 20:49)</a>:</h4>
<p><span class="user-mention silent" data-user-id="248906">cjgillot</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/239897831">said</a>:</p>
<blockquote>
<p>Running SimplifyCfg after the separate_const_switch passes allows ConstProp to pick up the optimization.</p>
</blockquote>
<p>Thank you, I will add that to the PR.</p>



<a name="239900406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239900406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239900406">(May 22 2021 at 21:10)</a>:</h4>
<p>Is there a way to be very explicit with LLVM that there are only two values that can reach a switch?</p>



<a name="239900654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239900654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239900654">(May 22 2021 at 21:14)</a>:</h4>
<p>Well, according to &lt;<a href="https://rust.godbolt.org/z/KqrYGxYW4">https://rust.godbolt.org/z/KqrYGxYW4</a>&gt; we already emit</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code>  <span class="nv nv-Anonymous">%6</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">i32</span><span class="p">,</span> <span class="kt">i32</span><span class="p">*</span> <span class="nv nv-Anonymous">%5</span><span class="p">,</span> <span class="k">align</span> <span class="m">4</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span><span class="p">,</span> <span class="nv">!range</span> <span class="nv nv-Anonymous">!11</span>
  <span class="nv">%_2</span> <span class="p">=</span> <span class="k">zext</span> <span class="kt">i32</span> <span class="nv nv-Anonymous">%6</span> <span class="k">to</span> <span class="kt">i64</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="k">switch</span> <span class="kt">i64</span> <span class="nv">%_2</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb2</span> <span class="p">[</span>
    <span class="kt">i64</span> <span class="m">0</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb3</span>
    <span class="kt">i64</span> <span class="m">1</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb1</span>
  <span class="p">],</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
</code></pre></div>
<p>where</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code><span class="nv nv-Anonymous">!11</span> <span class="p">=</span> <span class="p">!{</span><span class="kt">i32</span> <span class="m">0</span><span class="p">,</span> <span class="kt">i32</span> <span class="m">2</span><span class="p">}</span>
</code></pre></div>
<p>so I don't know that we can be much more explicit than that.</p>



<a name="239900786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239900786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239900786">(May 22 2021 at 21:16)</a>:</h4>
<p>That said, there's a potentially interesting (and potentially massive, I don't know) project there about removing the <code>zext</code> for that case, since it's clearly not useful, and it's possible that it's somehow interrupting LLVM's knowledge about the range</p>



<a name="239900919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239900919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239900919">(May 22 2021 at 21:19)</a>:</h4>
<p>(aka have the llvm codegen switch on the &lt;<a href="https://doc.rust-lang.org/nightly/std/marker/trait.DiscriminantKind.html">https://doc.rust-lang.org/nightly/std/marker/trait.DiscriminantKind.html</a>&gt; ::Discriminant instead of always switching on <code>isize</code>.)</p>



<a name="239901058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901058">(May 22 2021 at 21:21)</a>:</h4>
<p>isn't isize the discriminant type by default? that's what the MIR for that IR shows, anyway</p>



<a name="239901200"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901200" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901200">(May 22 2021 at 21:24)</a>:</h4>
<p>with repr(i32) on a substitute Result enum, the optimized IR doesn't change much:</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code> <span class="nv">%.sroa.0.0.extract.trunc</span> <span class="p">=</span> <span class="k">trunc</span> <span class="kt">i64</span> <span class="nv nv-Anonymous">%0</span> <span class="k">to</span> <span class="kt">i32</span>
  <span class="nv">%switch</span> <span class="p">=</span> <span class="k">icmp</span> <span class="k">eq</span> <span class="kt">i32</span> <span class="nv">%.sroa.0.0.extract.trunc</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="nv">%.0</span> <span class="p">=</span> <span class="k">select</span> <span class="kt">i1</span> <span class="nv">%switch</span><span class="p">,</span> <span class="kt">i32</span> <span class="m">123</span><span class="p">,</span> <span class="kt">i32</span> <span class="m">789</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="k">ret</span> <span class="kt">i32</span> <span class="nv">%.0</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!11</span>
</code></pre></div>



<a name="239901210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901210">(May 22 2021 at 21:24)</a>:</h4>
<p>as opposed to:</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code>  <span class="nv">%_2</span> <span class="p">=</span> <span class="k">and</span> <span class="kt">i64</span> <span class="nv nv-Anonymous">%0</span><span class="p">,</span> <span class="m">4294967295</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="nv">%switch</span> <span class="p">=</span> <span class="k">icmp</span> <span class="k">eq</span> <span class="kt">i64</span> <span class="nv">%_2</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="nv">%.0</span> <span class="p">=</span> <span class="k">select</span> <span class="kt">i1</span> <span class="nv">%switch</span><span class="p">,</span> <span class="kt">i32</span> <span class="m">123</span><span class="p">,</span> <span class="kt">i32</span> <span class="m">789</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="k">ret</span> <span class="kt">i32</span> <span class="nv">%.0</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!11</span>
</code></pre></div>



<a name="239901528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901528">(May 22 2021 at 21:30)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/239900654">said</a>:</p>
<blockquote>
<p>Well, according to &lt;<a href="https://rust.godbolt.org/z/KqrYGxYW4">https://rust.godbolt.org/z/KqrYGxYW4</a>&gt; we already emit</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code>  <span class="nv nv-Anonymous">%6</span> <span class="p">=</span> <span class="k">load</span> <span class="kt">i32</span><span class="p">,</span> <span class="kt">i32</span><span class="p">*</span> <span class="nv nv-Anonymous">%5</span><span class="p">,</span> <span class="k">align</span> <span class="m">4</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span><span class="p">,</span> <span class="nv">!range</span> <span class="nv nv-Anonymous">!11</span>
  <span class="nv">%_2</span> <span class="p">=</span> <span class="k">zext</span> <span class="kt">i32</span> <span class="nv nv-Anonymous">%6</span> <span class="k">to</span> <span class="kt">i64</span><span class="p">,</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
  <span class="k">switch</span> <span class="kt">i64</span> <span class="nv">%_2</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb2</span> <span class="p">[</span>
    <span class="kt">i64</span> <span class="m">0</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb3</span>
    <span class="kt">i64</span> <span class="m">1</span><span class="p">,</span> <span class="kt">label</span> <span class="nv">%bb1</span>
  <span class="p">],</span> <span class="nv">!dbg</span> <span class="nv nv-Anonymous">!10</span>
</code></pre></div>
<p>where</p>
<div class="codehilite" data-code-language="LLVM"><pre><span></span><code><span class="nv nv-Anonymous">!11</span> <span class="p">=</span> <span class="p">!{</span><span class="kt">i32</span> <span class="m">0</span><span class="p">,</span> <span class="kt">i32</span> <span class="m">2</span><span class="p">}</span>
</code></pre></div>
<p>so I don't know that we can be much more explicit than that.</p>
</blockquote>
<p>what is the meaning of <code>!</code> metadata in this content?</p>



<a name="239901536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901536">(May 22 2021 at 21:30)</a>:</h4>
<p>(I'm relatively new to LLVM IR as well)</p>



<a name="239901640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901640">(May 22 2021 at 21:32)</a>:</h4>
<p><code>!range !11</code> means that the loaded data lies in the range <code>!11</code>, which is defined as the interval <code>[0, 2)</code>.</p>



<a name="239901800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901800">(May 22 2021 at 21:35)</a>:</h4>
<p>Okay, that info gets completely removed after one of the following passes:<br>
0:  Instrument function entry/exit with calls to e.g. mcount() (pre inlining)<br>
1:  Simplify the CFG<br>
2:  SROA</p>



<a name="239901897"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901897" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901897">(May 22 2021 at 21:36)</a>:</h4>
<p>It's probably "Simplify the CFG" that does it</p>



<a name="239901904"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239901904" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239901904">(May 22 2021 at 21:36)</a>:</h4>
<p>maybe they just overlooked keeping the metadata</p>



<a name="239907209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239907209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239907209">(May 22 2021 at 23:12)</a>:</h4>
<p><code>!range</code> is unfortunately allowed only in rather limited situations: <a href="https://llvm.org/docs/LangRef.html#range-metadata">https://llvm.org/docs/LangRef.html#range-metadata</a></p>
<p>See <a href="https://github.com/rust-lang/rust/issues/49572#issuecomment-444858611">https://github.com/rust-lang/rust/issues/49572#issuecomment-444858611</a> for a related conversation about how LLVM loses the non-zero-ness of <code>NonZeroU32</code> and friends.</p>



<a name="239923538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239923538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239923538">(May 23 2021 at 04:43)</a>:</h4>
<p>Bummer</p>



<a name="239924545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239924545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239924545">(May 23 2021 at 05:03)</a>:</h4>
<p>I guess the only way forward now is to open an issue upstream</p>



<a name="239924554"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239924554" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239924554">(May 23 2021 at 05:03)</a>:</h4>
<p>I'll try to create a minimal IR file that illustrates the behavior</p>



<a name="239929413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239929413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239929413">(May 23 2021 at 06:42)</a>:</h4>
<p>Okay I think I have a good example of the bad behavior:</p>
<div class="codehilite"><pre><span></span><code>define i64 @demo(i64 %x) {
entry:
    switch i64 %x, label %bb3 [
        i64 0, label %bb1
        i64 1, label %bb2
    ]
bb1:
    ret i64 0
bb2:
    %0 = icmp eq i64 %x, 100 ; this will necessarily be false
    br i1 %0, label %bb5, label %bb4
bb3:
    unreachable
bb4:
    ret i64 200
bb5:
    ret i64 1
}
</code></pre></div>
<p>gets turned into</p>
<div class="codehilite"><pre><span></span><code>; ModuleID = &#39;repro.ll&#39;
source_filename = &quot;repro.ll&quot;

define i64 @demo(i64 %x) {
entry:
  %switch = icmp ult i64 %x, 1
  %0 = icmp eq i64 %x, 100 ; this cmp should be removed later on but the information is lost
  %. = select i1 %0, i64 1, i64 200
  %merge = select i1 %switch, i64 0, i64 %.
  ret i64 %merge
}
</code></pre></div>



<a name="239929424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239929424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239929424">(May 23 2021 at 06:42)</a>:</h4>
<p>in that transformation, all information about %x being in [0; 1] is lost</p>



<a name="239929437"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239929437" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239929437">(May 23 2021 at 06:42)</a>:</h4>
<p>can you confirm I'm not missing anything here?</p>



<a name="239929872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239929872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239929872">(May 23 2021 at 06:53)</a>:</h4>
<p>(it's generating suboptimal code and SimplifyCFG lost the information to optimize that away)</p>



<a name="239930655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239930655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239930655">(May 23 2021 at 07:09)</a>:</h4>
<p>Hmm, I wonder what happens if it's <code>i1</code> instead of <code>i64</code>.  Dunno if it'd be worth special-casing 2-variant enums, but maybe...</p>



<a name="239930698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239930698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239930698">(May 23 2021 at 07:10)</a>:</h4>
<p>(0- and 1-variant enums are already special, but in very different ways.)</p>



<a name="239930802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239930802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239930802">(May 23 2021 at 07:12)</a>:</h4>
<p>with i1, the cmp is correctly optimized away</p>



<a name="239930821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239930821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239930821">(May 23 2021 at 07:13)</a>:</h4>
<p>but it's a bit unfortunate that we would have to 1. specialize 2. have it only work on 2-variant enums when we could definitely profit from that in 3-variant cases for example</p>



<a name="239931018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931018">(May 23 2021 at 07:17)</a>:</h4>
<p>I'm waiting for an account creation on the LLVM bugtracker, and once it's done I'll submit the problem there</p>



<a name="239931256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931256">(May 23 2021 at 07:21)</a>:</h4>
<p>Does LLVM do that transform on a 3-option <code>switch</code>?  I wonder if it's the <code>switch</code>-&gt;<code>select</code> transformation that's causing the problem, which would be 2-variant specific.</p>



<a name="239931261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931261">(May 23 2021 at 07:21)</a>:</h4>
<p>ah that makes sense</p>



<a name="239931303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931303">(May 23 2021 at 07:22)</a>:</h4>
<p>let me try</p>



<a name="239931320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931320">(May 23 2021 at 07:22)</a>:</h4>
<p>(As usual I'm just guessing here -- I'm no expert in LLVM, nor MIR.)</p>



<a name="239931340"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931340" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931340">(May 23 2021 at 07:23)</a>:</h4>
<p>On 3-variants it does that too (using a pretty smart trick! it subtracks an offset so variant 0 is negative, variant 1 is 0 et variant 2 is positive)</p>



<a name="239931488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931488">(May 23 2021 at 07:25)</a>:</h4>
<p>it's doing it on many variant amounts, it can figure out tricks pretty often</p>



<a name="239931569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931569">(May 23 2021 at 07:27)</a>:</h4>
<p>I think the solution to our problem could simply be for SimplifyCFG to not forget the unreachable case</p>



<a name="239931587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239931587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239931587">(May 23 2021 at 07:27)</a>:</h4>
<p>Is it LLVM that decides a SimplifyCFG pass should be ran early, or is that decision made by the front-end user? If so, changing that ordering could maybe affect those results</p>



<a name="239932680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239932680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239932680">(May 23 2021 at 07:49)</a>:</h4>
<p>Total shot in the dark: you could see if <code>-Z new-llvm-pass-manager</code> makes a difference.</p>
<p>AFAIK, we could control the passes more in depth, but generally prefer to follow the order that clang does for C++, as one that's already well tested.</p>



<a name="239933102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239933102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239933102">(May 23 2021 at 07:59)</a>:</h4>
<p>no luck with the new manager</p>



<a name="239934032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/239934032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#239934032">(May 23 2021 at 08:14)</a>:</h4>
<p>Anyway: this is a blocker. I don't think we can afford the desugaring to regress into a jump. Hopefully we can get some advice for a temporary alternative from LLVM people.</p>



<a name="240064399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240064399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240064399">(May 24 2021 at 15:12)</a>:</h4>
<p><a href="https://bugs.llvm.org/show_bug.cgi?id=50455">https://bugs.llvm.org/show_bug.cgi?id=50455</a></p>



<a name="240064413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240064413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240064413">(May 24 2021 at 15:13)</a>:</h4>
<p>to be continued</p>



<a name="240095008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240095008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240095008">(May 24 2021 at 18:54)</a>:</h4>
<p>Would it make sense to PR what I have in a state where it is not enabled by default until we have this fixed in LLVM?</p>



<a name="240096127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240096127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240096127">(May 24 2021 at 19:01)</a>:</h4>
<p>Yes. A lot of mir-opts are disabled by default.</p>



<a name="240115638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240115638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240115638">(May 24 2021 at 21:38)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/85646">https://github.com/rust-lang/rust/pull/85646</a></p>



<a name="240115648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240115648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240115648">(May 24 2021 at 21:38)</a>:</h4>
<p>It's up!</p>



<a name="240223572"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240223572" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240223572">(May 25 2021 at 17:37)</a>:</h4>
<p><span class="user-mention" data-user-id="248906">@cjgillot</span> For some reason I cannot reproduce your results with the SiplifyCfg pass to make ConstProp pick up the optimization, even though I had before. Did you do anything beyond adding an instance of the pass where I added it in the PR?</p>



<a name="240238005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240238005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240238005">(May 25 2021 at 19:30)</a>:</h4>
<p>Did you push changes to the PR, with the diff of the ConstProp pass?</p>



<a name="240248553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240248553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240248553">(May 25 2021 at 20:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="248906">cjgillot</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/240238005">said</a>:</p>
<blockquote>
<p>Did you push changes to the PR, with the diff of the ConstProp pass?</p>
</blockquote>
<p>I did not but the diff is a no-op</p>



<a name="240248635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240248635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240248635">(May 25 2021 at 20:54)</a>:</h4>
<p>I'm accumulating a bunch of fixes for your comments and that's why I ended up finding out it was not picking it up anymore</p>



<a name="240248690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240248690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240248690">(May 25 2021 at 20:54)</a>:</h4>
<p>The annoying part is that I did not change what gets output for that specific example</p>



<a name="240248707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240248707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240248707">(May 25 2021 at 20:54)</a>:</h4>
<p>Maybe i'm just misconfiguring it or something, I'm going to push things</p>



<a name="240248773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240248773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240248773">(May 25 2021 at 20:55)</a>:</h4>
<p>(by the way, about the LLVM opt: apparently the range metadata does not get eliminated in the first passes as my CFG visualization suggested, so I'll dig into that some more)</p>



<a name="240249186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240249186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240249186">(May 25 2021 at 20:58)</a>:</h4>
<p>Does it work after making function monomorphic?</p>



<a name="240249924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240249924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240249924">(May 25 2021 at 21:04)</a>:</h4>
<p>The PR uses generic <code>Result&lt;T, E&gt;</code> while earlier example in this thread was using <code>Result&lt;i32, u32&gt;</code>.</p>



<a name="240250008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240250008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240250008">(May 25 2021 at 21:04)</a>:</h4>
<p>oh wow, it does</p>



<a name="240250019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240250019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240250019">(May 25 2021 at 21:04)</a>:</h4>
<p>that's interesting</p>



<a name="240250028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240250028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240250028">(May 25 2021 at 21:05)</a>:</h4>
<p>why would it depend on that?</p>



<a name="240250050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240250050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240250050">(May 25 2021 at 21:05)</a>:</h4>
<p>(you seem to be very knowledgeable of how ConstProp works)</p>



<a name="240252701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240252701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240252701">(May 25 2021 at 21:29)</a>:</h4>
<p>okay now that's really interesting: you don't even need to simplify for it to work, but it only works when it's monomorphic</p>



<a name="240252784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/240252784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#240252784">(May 25 2021 at 21:30)</a>:</h4>
<p>sounds like a ConstProp problem if anything</p>



<a name="242101128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242101128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242101128">(Jun 09 2021 at 17:24)</a>:</h4>
<p>An update on this: a student friend of mine coincidentally stumbled upon my LLVM bug report and suggested adding a <code>-lowerswitch</code> pass before SimplifyCFG in LLVM to make sure the unreachable is not lost</p>



<a name="242101181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242101181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242101181">(Jun 09 2021 at 17:25)</a>:</h4>
<p>I'll try doing this to see if it does the trick</p>



<a name="242101209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242101209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242101209">(Jun 09 2021 at 17:25)</a>:</h4>
<p>Sorry for not updating the PR lately, I've been pretty busy recently (in LLVM, actually) for my current internship</p>



<a name="242110180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242110180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242110180">(Jun 09 2021 at 18:33)</a>:</h4>
<p>I updated the PR with some more git troubles, if anyone has some time to help me resolve it.</p>



<a name="242110223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242110223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242110223">(Jun 09 2021 at 18:34)</a>:</h4>
<p>If not, i'll just copy my changes to a different branch and start over, but that's a bit sad. Unless I can force push a completely different branch to the currently opened branch PR, which would not be too bad.</p>



<a name="242113538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242113538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242113538">(Jun 09 2021 at 18:59)</a>:</h4>
<p><span class="user-mention silent" data-user-id="409735">Théo Degioanni</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133/near/242110223">said</a>:</p>
<blockquote>
<p>Unless I can force push a completely different branch to the currently opened branch PR, which would not be too bad.</p>
</blockquote>
<p>You absolutely can.  Which local branch pushes to which remote branch is just metadata -- you can see what goes where in your <code>.git/config</code>.  (See also the <code>--set-upstream</code> flag on <a href="https://git-scm.com/docs/git-push">git push</a>.)</p>



<a name="242123813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242123813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242123813">(Jun 09 2021 at 20:17)</a>:</h4>
<p>Okay, great. I’ll do that.</p>



<a name="242139195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242139195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242139195">(Jun 09 2021 at 22:29)</a>:</h4>
<p>unfortunately lowerswitch has the same information loss issue</p>



<a name="242139272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242139272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242139272">(Jun 09 2021 at 22:30)</a>:</h4>
<p>this can probably be fixed in LLVM, but for now I'll try to get that PR mergeable</p>



<a name="242140064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242140064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242140064">(Jun 09 2021 at 22:39)</a>:</h4>
<p>PR is up to date<br>
<a href="https://github.com/rust-lang/rust/pull/85646">https://github.com/rust-lang/rust/pull/85646</a></p>



<a name="242140141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242140141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242140141">(Jun 09 2021 at 22:40)</a>:</h4>
<p>wow scottmcm you were fast</p>



<a name="242140676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242140676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242140676">(Jun 09 2021 at 22:47)</a>:</h4>
<p>I just noticed a suboptimal aspect of the opt</p>



<a name="242140748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242140748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242140748">(Jun 09 2021 at 22:48)</a>:</h4>
<p>the .skip(1) change is not equivalent to my initial proposal, as if the first block is the only block to set the value as a const, it misses the optimisation</p>



<a name="242140756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242140756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242140756">(Jun 09 2021 at 22:48)</a>:</h4>
<p>i'll restore the previous approach</p>



<a name="242141624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242141624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242141624">(Jun 09 2021 at 23:00)</a>:</h4>
<p>...for what it's worth the outer first loop is immutable and thus trivial to make parallel...</p>



<a name="242141766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242141766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242141766">(Jun 09 2021 at 23:02)</a>:</h4>
<p><code>blocks.par_iter().filter_map(find_edges).flatten().collect();</code></p>



<a name="242232097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242232097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242232097">(Jun 10 2021 at 16:15)</a>:</h4>
<p>I've tried the pass against <a href="https://github.com/rust-lang/rust/issues/85365">https://github.com/rust-lang/rust/issues/85365</a><br>
While it does not seem to help with the copying, it seems to produce more efficient assembly somehow? This is one of the reasons I'd like to find a way to bench it against real code</p>



<a name="242232353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242232353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242232353">(Jun 10 2021 at 16:17)</a>:</h4>
<p>Like maybe it does make for a worst <code>Ok(x?)</code> currently but it might also already help on other fronts? Or maybe it's doing absolutely terrible in other places as well. I'd like to know. I'll bench it with a few projects I can find that have benchmarks, but iirc the CI setup has some perf tests that can be ran, is that right?</p>



<a name="242235140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242235140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242235140">(Jun 10 2021 at 16:38)</a>:</h4>
<p>that is absolutely right, CI does have some automated rustc benchmarks</p>



<a name="242235621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242235621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242235621">(Jun 10 2021 at 16:42)</a>:</h4>
<p>and since I assume you'd like to run it on <a href="https://github.com/rust-lang/rust/issues/85646">#85646</a> I can do that for you</p>



<a name="242235678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242235678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242235678">(Jun 10 2021 at 16:42)</a>:</h4>
<p>however, I see the cargo.lock commit and revert, so if you could just squash the "un-update itertools" commit and force push, I'll launch the perf run right after</p>



<a name="242235746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242235746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242235746">(Jun 10 2021 at 16:43)</a>:</h4>
<p>it's not a requirement for the perf run of course, but this commit will have to be squashed anyway before the PR being merged, so might as well take care of it now :)</p>



<a name="242237311"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237311" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237311">(Jun 10 2021 at 16:55)</a>:</h4>
<p>I'll do this right away, thanks!</p>



<a name="242237381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237381">(Jun 10 2021 at 16:56)</a>:</h4>
<p>(mais de rien :)</p>



<a name="242237563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237563">(Jun 10 2021 at 16:57)</a>:</h4>
<p>squashed!</p>



<a name="242237612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237612">(Jun 10 2021 at 16:58)</a>:</h4>
<p>considering the LLVM issue I expect the perf to be regressing overall unfortunately</p>



<a name="242237660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237660">(Jun 10 2021 at 16:58)</a>:</h4>
<p>but it's good to have data on it</p>



<a name="242237765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237765">(Jun 10 2021 at 16:58)</a>:</h4>
<p>here we gooo</p>



<a name="242237826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237826">(Jun 10 2021 at 16:59)</a>:</h4>
<p>I'll let CI run a bit for formatting reasons and so on before launching the perf run, but I'm on it</p>



<a name="242237865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242237865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242237865">(Jun 10 2021 at 16:59)</a>:</h4>
<p>quite possible, also sometimes rustc is not that representative of a workload</p>



<a name="242238031"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238031" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238031">(Jun 10 2021 at 17:00)</a>:</h4>
<p>is your optimization hooked up to run all the time ? or gated being a specific mir-opt-level ?</p>



<a name="242238072"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238072" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238072">(Jun 10 2021 at 17:00)</a>:</h4>
<p>mir-opt-level 4</p>



<a name="242238140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238140">(Jun 10 2021 at 17:01)</a>:</h4>
<p>so you're looking for possible regressions even if it doesn't run ?</p>



<a name="242238174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238174">(Jun 10 2021 at 17:01)</a>:</h4>
<p>(since it wouldn't run by default on the perf run we'll launch)</p>



<a name="242238299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238299">(Jun 10 2021 at 17:02)</a>:</h4>
<p>ah no, I wanted to look at possible regressions if it does run</p>



<a name="242238317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238317">(Jun 10 2021 at 17:02)</a>:</h4>
<p>hopefully there would be none in smaller opt-levels</p>



<a name="242238446"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238446" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238446">(Jun 10 2021 at 17:03)</a>:</h4>
<p>is it possible to do a run with opt level 4?</p>



<a name="242238530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238530">(Jun 10 2021 at 17:04)</a>:</h4>
<p>it would be easier to add a temporary commit just for the run that lowers the opt gating</p>



<a name="242238594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238594">(Jun 10 2021 at 17:04)</a>:</h4>
<p>(and one'd drop the commit after the perf runs are done so that it doesn't land as-is)</p>



<a name="242238621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238621">(Jun 10 2021 at 17:05)</a>:</h4>
<p>gotcha, doing it</p>



<a name="242238833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238833">(Jun 10 2021 at 17:06)</a>:</h4>
<p>done</p>



<a name="242238998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242238998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242238998">(Jun 10 2021 at 17:07)</a>:</h4>
<p>ok so it'll run for all benches (maybe even check runs)</p>



<a name="242239027"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239027" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239027">(Jun 10 2021 at 17:08)</a>:</h4>
<p>should be good enough to have some data</p>



<a name="242239082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239082">(Jun 10 2021 at 17:08)</a>:</h4>
<p>and if not we'll adjust and do other runs</p>



<a name="242239527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239527">(Jun 10 2021 at 17:10)</a>:</h4>
<p>interesting that GH disabled CI as soon as you pushed, so I suggest not touching the PR for the time being, lest it goes back to the unapproved state (it shouldn't matter for the try and perf runs but who knows)</p>



<a name="242239589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239589">(Jun 10 2021 at 17:11)</a>:</h4>
<p>until we have the perf run data, that is</p>



<a name="242239604"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239604" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239604">(Jun 10 2021 at 17:11)</a>:</h4>
<p>okay!</p>



<a name="242239630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239630">(Jun 10 2021 at 17:11)</a>:</h4>
<p>in any case, it's queued</p>



<a name="242239796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242239796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242239796">(Jun 10 2021 at 17:12)</a>:</h4>
<p>and <span class="user-mention" data-user-id="248906">@cjgillot</span> is on top of things for the review, so let's celebrate with another <span aria-label="baguette" class="emoji emoji-1f956" role="img" title="baguette">:baguette:</span></p>



<a name="242240675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242240675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242240675">(Jun 10 2021 at 17:18)</a>:</h4>
<p><span class="user-mention" data-user-id="248906">@cjgillot</span> what does "place projection" precisely mean?</p>



<a name="242240704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242240704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242240704">(Jun 10 2021 at 17:18)</a>:</h4>
<p>I find the "only discriminant" aspect could be more precise generally in the code I currently have</p>



<a name="242240783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242240783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242240783">(Jun 10 2021 at 17:19)</a>:</h4>
<p>like for example if we're only tracking the discriminant in the determining place, surely the <code>is_likely_const</code> function would like to know about that in some way</p>



<a name="242241362"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242241362" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242241362">(Jun 10 2021 at 17:23)</a>:</h4>
<p>(I'm a bit puzzled  by the CI failure)</p>



<a name="242241491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242241491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242241491">(Jun 10 2021 at 17:24)</a>:</h4>
<p>A MIR Place is a Local along with projections. Projections can be a Deref (*_1), a Field access (_1.0 as type), no projection (just _1), etc. As written, find_determining_place only works as long there is no projection anywhere.</p>



<a name="242241731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242241731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242241731">(Jun 10 2021 at 17:26)</a>:</h4>
<p>ah yes, I think this was part of the heuristic nature, to not have to handle aliasing and things like that</p>



<a name="242241782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242241782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242241782">(Jun 10 2021 at 17:26)</a>:</h4>
<p>but if there's a way that is not too slow to do it i'd like to try</p>



<a name="242241857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242241857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242241857">(Jun 10 2021 at 17:27)</a>:</h4>
<p>You can just check there is no such projections.</p>



<a name="242242629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242242629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242242629">(Jun 10 2021 at 17:33)</a>:</h4>
<p>Likewise for is_likely_const.</p>



<a name="242243448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242243448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242243448">(Jun 10 2021 at 17:38)</a>:</h4>
<p>Speaking of which, I think CopyNonOverlapping is fine and can just be ignored, it's not going to affect what is happening, surely</p>



<a name="242243479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242243479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242243479">(Jun 10 2021 at 17:39)</a>:</h4>
<p>it's only touching memory, I mean</p>



<a name="242243541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242243541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242243541">(Jun 10 2021 at 17:39)</a>:</h4>
<p>I rejected it because I wasn't sure and forgot to mention it</p>



<a name="242246692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242246692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242246692">(Jun 10 2021 at 18:03)</a>:</h4>
<p><span class="user-mention" data-user-id="248906">@cjgillot</span> not super sure how to handle projections... my understanding is that places are a projection over a value, so I don't understand what is not handled in a satisfactory way currently</p>



<a name="242246749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242246749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242246749">(Jun 10 2021 at 18:03)</a>:</h4>
<p>I think my understanding of places might be incorrect as they are typed with a list of projections, not just one</p>



<a name="242247828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242247828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242247828">(Jun 10 2021 at 18:12)</a>:</h4>
<p>For the most part, you can just bail out of the optimization when there is an projection you can't handle.</p>



<a name="242248155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242248155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242248155">(Jun 10 2021 at 18:14)</a>:</h4>
<p>Right, but is there some documentation about the projection abstraction? I missed it entirely while making the pass and it is not obvious to me how it works</p>



<a name="242249297"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242249297" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242249297">(Jun 10 2021 at 18:22)</a>:</h4>
<p>There is a brief introduction in the dev guide <a href="https://rustc-dev-guide.rust-lang.org/mir/index.html">https://rustc-dev-guide.rust-lang.org/mir/index.html</a><br>
Otherwise there is the rustc's rustdoc.</p>



<a name="242251715"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242251715" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242251715">(Jun 10 2021 at 18:38)</a>:</h4>
<p>What does it mean for a place to have multiple projections?</p>



<a name="242251802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242251802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242251802">(Jun 10 2021 at 18:39)</a>:</h4>
<p>One after the other like <code>((*_1).0)[3]</code>.</p>



<a name="242251848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242251848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242251848">(Jun 10 2021 at 18:39)</a>:</h4>
<p>Oh okay!</p>



<a name="242251867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242251867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242251867">(Jun 10 2021 at 18:39)</a>:</h4>
<p>Thanks!</p>



<a name="242251973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242251973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242251973">(Jun 10 2021 at 18:40)</a>:</h4>
<p>I’ll fix this asap</p>



<a name="242459492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242459492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242459492">(Jun 12 2021 at 16:29)</a>:</h4>
<p>I might want to add a method on Place <code>within(&amp;self, other: &amp;Place) -&gt; Option&lt;bool&gt;</code> that attempts to compute if the self place is a part of the other place, returning none if it can’t tell (for example if it does dereferencing)</p>



<a name="242459493"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242459493" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242459493">(Jun 12 2021 at 16:29)</a>:</h4>
<p>This might be useful to other opts</p>



<a name="242459562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242459562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242459562">(Jun 12 2021 at 16:30)</a>:</h4>
<p>That would make the change to support projections in the opt rather clean</p>



<a name="242459592"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242459592" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242459592">(Jun 12 2021 at 16:31)</a>:</h4>
<p>(Instead of comparing target place with tracked place, I would check whether tracked place is within target place)</p>



<a name="242459661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242459661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242459661">(Jun 12 2021 at 16:33)</a>:</h4>
<p>I wonder if this actually is the semantics of the the partialord impl on Place</p>



<a name="242460151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242460151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242460151">(Jun 12 2021 at 16:44)</a>:</h4>
<p>it's hard to tell, that would be unlikely however</p>



<a name="242463195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242463195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242463195">(Jun 12 2021 at 17:58)</a>:</h4>
<p>why is discriminant not a projection?</p>



<a name="242463322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242463322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242463322">(Jun 12 2021 at 18:00)</a>:</h4>
<p>Because the discriminant often isn't stored at a specific offset, but needs to be calculated.</p>



<a name="242463351"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242463351" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242463351">(Jun 12 2021 at 18:01)</a>:</h4>
<p>For example for <code>Option&lt;&amp;u8&gt;</code> the calculation for the discriminant is likely pointer sized word == 0 =&gt; <code>None</code> else <code>Some</code>.</p>



<a name="242463964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/242463964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#242463964">(Jun 12 2021 at 18:14)</a>:</h4>
<p>thank you!</p>



<a name="243179981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243179981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243179981">(Jun 18 2021 at 16:00)</a>:</h4>
<p>is it guaranteed that when an enum place is affected, the content will always be set before the discriminant?</p>



<a name="243180308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243180308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243180308">(Jun 18 2021 at 16:03)</a>:</h4>
<p>or, actually, here is a better question: is it guaranteed that an enum affectation will be associated with a <code>discriminant(_x) = n</code>? I've tried with <code>Option&lt;&amp;u32&gt;</code> which generated one, but I'm not sure if it is guaranteed to be</p>



<a name="243180578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243180578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243180578">(Jun 18 2021 at 16:05)</a>:</h4>
<p>I don't think you should rely on either of those things.  This is an optimization, so it just needs to be sound, not complete.  If it occasionally duplicates a terminal in a place that doesn't optimize that's fine, and if it occasionally doesn't duplicate the terminal in a place where it could have that's also fine.</p>



<a name="243180708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243180708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243180708">(Jun 18 2021 at 16:06)</a>:</h4>
<p>right</p>



<a name="243180743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243180743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243180743">(Jun 18 2021 at 16:07)</a>:</h4>
<p>I'll just fix the discriminant tracking mistake and not care about projections, then</p>



<a name="243181444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243181444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243181444">(Jun 18 2021 at 16:14)</a>:</h4>
<p>(I'm not entirely sure what you mean by "enum affectation" -- certainly <code>_x = _y</code> can also change the discriminant.  And with <code>#[repr(C, u8)]</code> it can be set by writing through a projection.)</p>



<a name="243181580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243181580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243181580">(Jun 18 2021 at 16:15)</a>:</h4>
<p>(I meant enum construction, I think)</p>



<a name="243181684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243181684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243181684">(Jun 18 2021 at 16:16)</a>:</h4>
<p>(like, when one does <code>let x: Option&lt;&amp;u32&gt; = Some(&amp;y)</code>)</p>



<a name="243181780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243181780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243181780">(Jun 18 2021 at 16:17)</a>:</h4>
<p>(technically I guess setting the discriminant explicitly could not be necessary, because we're setting the enum to be a non-null value and it's using a niche)</p>



<a name="243181827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243181827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243181827">(Jun 18 2021 at 16:17)</a>:</h4>
<p>(I don't know if other parts of the compiler rely on the discriminant being set anyway)</p>



<a name="243182404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243182404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243182404">(Jun 18 2021 at 16:23)</a>:</h4>
<p>I'm reasonably confident that an enum variant literal will always set it, because the code generating the MIR from HIR doesn't want to bother checking the layout details to know whether it's done via a niche.</p>
<p>But I'd still say you shouldn't rely on that for <em>soundness</em> of the optimization.  Just as a nice heuristic for deciding whether to run it.</p>



<a name="243183030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243183030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243183030">(Jun 18 2021 at 16:29)</a>:</h4>
<p>yes it was definitely not for soundness</p>



<a name="243183099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243183099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243183099">(Jun 18 2021 at 16:30)</a>:</h4>
<p>I just wanted to make the analysis finer to address a review on the PR</p>



<a name="243183678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243183678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243183678">(Jun 18 2021 at 16:36)</a>:</h4>
<p>Depending on where this runs as part of the MIR opt pipeline, I could see things like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">42</span><span class="p">);</span><span class="w"></span>
<span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">21</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>not setting the discriminant to the same value twice but I don't think we currently have any optimizations that would perform that transformation.</p>



<a name="243183757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243183757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243183757">(Jun 18 2021 at 16:37)</a>:</h4>
<p>Okay, I wanted at least one so it would have been good. But i'm dropping this because I think it's going to be needlessly complex</p>



<a name="243183787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/243183787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#243183787">(Jun 18 2021 at 16:37)</a>:</h4>
<p>Like the approach I wanted would cover cases that would never show up unless I added significant complexity</p>



<a name="245731650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/245731650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Michael Maitland <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#245731650">(Jul 12 2021 at 18:58)</a>:</h4>
<p>Hi, I'm looking for a first issue to contribute to. Is this something that would be a first good issue / is it free to pick up? I saw someone started working on it and then closed it</p>



<a name="246379208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing%20a%20match%20on%20a%20just-constructed%20enum%20%2385133/near/246379208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Théo Degioanni <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/Optimizing.20a.20match.20on.20a.20just-constructed.20enum.20.2385133.html#246379208">(Jul 18 2021 at 13:37)</a>:</h4>
<p>Nothing is closed afaik, the PR for the first stab at this is still open <a href="https://github.com/rust-lang/rust/pull/85646">https://github.com/rust-lang/rust/pull/85646</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>